locked
Need Help converting VB.NET Webservice to C#. RRS feed

  • Question

  • User-666661156 posted

    Hey Everyone, 

    Trying to implement this VB.NET web method in a C# MVC application for uploading documents. Need some assistance for converting the code: 

    'filePath would be the file that's being uploaded. 
    Dim fInfo As New FileInfo(filePath) Dim numBytes As Long = fInfo.Length Dim fStream As New FileStream(filePath, FileMode.Open, FileAccess.Read) Dim br As New BinaryReader(fStream) Dim data As Byte() = br.ReadBytes(CInt(numBytes)) Dim xmlUploadResult As System.Xml.XmlNode Dim dsWSdev As New WebFileStorage.Service dsWSdev.Credentials = System.Net.CredentialCache.DefaultCredentials xmlUploadResult = dsWSdev.ApplyUpload("UserName", "Password", "Document", "Name", "Val", "False", "Folder", "FileName", data) Dim strResult As String = xmlUploadResult.FirstChild.InnerText.ToString If Not strResult = "200" Then Throw New ArgumentException("Error uploading the document: " & xmlUploadResult.InnerText.ToString) End If Private Function convertToBytes(ByVal p As String) As Byte() Dim fs As New System.IO.FileStream(p, IO.FileMode.Open, IO.FileAccess.Read) Dim br As New System.IO.BinaryReader(fs) Dim bytes As Byte() = br.ReadBytes(CType(fs.Length, Integer)) fs.Flush() fs.Close() br.Close() Return bytes End Function

    Any help or insight would be appreciated, thanks! 

    Tuesday, May 1, 2018 8:00 PM

Answers

  • User283571144 posted

    Hi rankone,

    Trying to implement this VB.NET web method in a C# MVC application for uploading documents. Need some assistance for converting the code: 

    According to your description, I suggest you could try below codes:

    MVC controller:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Xml;
    
    namespace WebApiNormall.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                string filePath = "";
                FileInfo fInfo = new FileInfo(filePath);
                long numBytes = fInfo.Length;
                FileStream fStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fStream);
                Byte[] data = br.ReadBytes(Convert.ToInt32(numBytes));
                XmlNode xmlUploadResult;
                //Since I don't have the WebFileStorage library, I could only directy convert vb code to C#
                WebFileStorage.Service dsWSdev = new WebFileStorage.Service();
                dsWSdev.Credentials = System.Net.CredentialCache.DefaultCredentials;
                xmlUploadResult = dsWSdev.ApplyUpload("UserName", "Password", "Document", "Name", "Val", "False", "Folder", "FileName", data);
    
                string strResult = xmlUploadResult.FirstChild.InnerText.ToString();
    
                if (strResult != "200")
                {
                    throw new ArgumentException("Error uploading the document: " + xmlUploadResult.InnerText.ToString());
                }
    
                ViewBag.Title = "Home Page";
    
                return View();
            }
            private byte[] convertToBytes(string p)
            {
                FileStream fs = new FileStream(p, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fs);
                byte[] bytes = br.ReadBytes((int)fs.Length);
                fs.Flush();
                fs.Close();
                br.Close();
                return bytes;
            }
    
        }
    }
    

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 2, 2018 2:45 AM
  • User303363814 posted

    The FileInfo constructor takes a string which is the name of the file.  Your file variable is of type HttpPostedFileBase which is not a string giving the name of the file.  As you have shown in the next line of code, the way to get the name of the file is with the .FileName property.  So, your line should be

    FileInfo fInfo = new FileInfo(file.FileName);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 4, 2018 3:29 AM

All replies

  • User2053451246 posted

    Try http://converter.telerik.com

    Tuesday, May 1, 2018 8:14 PM
  • User-666661156 posted

    I tried that, but it keeps giving me a void error. I think the converter requires all the imports in VB.NET to be added, which I don't have access to currently. Such as imports System.IO

    Tuesday, May 1, 2018 8:17 PM
  • User2053451246 posted

    I don't think it requires all the imports.  But if you simply pasted the code in your original post it would error out.  I don't think it's complete.  There are those If...then statements not inside of any function, which I believe is invalid VB.

    Tuesday, May 1, 2018 8:41 PM
  • User283571144 posted

    Hi rankone,

    Trying to implement this VB.NET web method in a C# MVC application for uploading documents. Need some assistance for converting the code: 

    According to your description, I suggest you could try below codes:

    MVC controller:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Xml;
    
    namespace WebApiNormall.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                string filePath = "";
                FileInfo fInfo = new FileInfo(filePath);
                long numBytes = fInfo.Length;
                FileStream fStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fStream);
                Byte[] data = br.ReadBytes(Convert.ToInt32(numBytes));
                XmlNode xmlUploadResult;
                //Since I don't have the WebFileStorage library, I could only directy convert vb code to C#
                WebFileStorage.Service dsWSdev = new WebFileStorage.Service();
                dsWSdev.Credentials = System.Net.CredentialCache.DefaultCredentials;
                xmlUploadResult = dsWSdev.ApplyUpload("UserName", "Password", "Document", "Name", "Val", "False", "Folder", "FileName", data);
    
                string strResult = xmlUploadResult.FirstChild.InnerText.ToString();
    
                if (strResult != "200")
                {
                    throw new ArgumentException("Error uploading the document: " + xmlUploadResult.InnerText.ToString());
                }
    
                ViewBag.Title = "Home Page";
    
                return View();
            }
            private byte[] convertToBytes(string p)
            {
                FileStream fs = new FileStream(p, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fs);
                byte[] bytes = br.ReadBytes((int)fs.Length);
                fs.Flush();
                fs.Close();
                br.Close();
                return bytes;
            }
    
        }
    }
    

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 2, 2018 2:45 AM
  • User-666661156 posted

    Hi rankone,

    rankone

    Trying to implement this VB.NET web method in a C# MVC application for uploading documents. Need some assistance for converting the code: 

    According to your description, I suggest you could try below codes:

    Best Regards,

    Brando

    Appreciate the help Brando, quick question, as I was working on the conversion myself and trying to fit the code for my needs. Any idea how I could go about implementing the filePath(uploaded file) as part of IEnumerable<HttpPostedFileBase> files ? As I'm guessing the webservice code above would have to be called within the following:

    [HttpPost]
    public ActionResult Multiple(IEnumerable<HttpPostedFileBase> files)
    {
        foreach (var file in files)
        {
            if (file != null && file.ContentLength > 0)
            {
                file.SaveAs(webServiceURLorPath, Guid.NewGuid() + Path.GetExtension(file.FileName)));
            }
        }
        return View();
    }

    So if I try 

    FileInfo fInfo = new FileInfo(file);

    I get the file as type IEnumerable cannot be converted to type string error. This essentially is the file that's being uploaded so was just wondering if there's a way around it ? 

    Wednesday, May 2, 2018 3:32 AM
  • User303363814 posted

    So if I try 

    FileInfo fInfo = new FileInfo(file);

    Where did you put this line?  Show your code

    Thursday, May 3, 2018 3:58 AM
  • User-666661156 posted

    Where did you put this line?  Show your code

    Basically fInfo is the file that's being uploaded, so I was trying to put it in my existing code for a MVC multi file uploader and got the above mentioned error 

    [HttpPost]
    public ActionResult Multiple(IEnumerable<HttpPostedFileBase> files)
    {
        foreach (var file in files)
        {
            if (file != null && file.ContentLength > 0)
            {
    FileInfo fInfo = new FileInfo(file)
                file.SaveAs(webServiceURLorPath, Guid.NewGuid() +   Path.GetExtension(file.FileName)));
            }
        }
        return View();
    }
    

    Thursday, May 3, 2018 11:19 PM
  • User303363814 posted

    The FileInfo constructor takes a string which is the name of the file.  Your file variable is of type HttpPostedFileBase which is not a string giving the name of the file.  As you have shown in the next line of code, the way to get the name of the file is with the .FileName property.  So, your line should be

    FileInfo fInfo = new FileInfo(file.FileName);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 4, 2018 3:29 AM
  • User-666661156 posted

    I've tried implementing the following after adding the WebService Reference to the solution. Not sure if there's an issue with the code(due to the conversion) or with the WebService itself. As per the ArgumentException the error I get is 761DATABASE_NONEXISTINGPROPERTYCOLUMN_ERROR

    Here is the code: 

            [HttpPost]
            public ActionResult Multiple(IEnumerable<HttpPostedFileBase> files)
            {
                foreach (var file in files)
                {
                    if (file != null && file.ContentLength > 0)
                    {
    
                        //Convert file to Base64binary for upload using Docushare web services
                        FileInfo fInfo = new FileInfo(file.FileName);
                        long numBytes = fInfo.Length;
                        FileStream fStream = new FileStream(file.FileName, FileMode.Open, FileAccess.Read);
                        BinaryReader br = new BinaryReader(fStream);
                        byte[] data = br.ReadBytes(Convert.ToInt32(numBytes));
    
                        //Upload the File
                        System.Xml.XmlNode xmlUploadResult;
                        DocuShareWebServiceReference.Service dsWSdev = new DocuShareWebServiceReference.Service();
                        xmlUploadResult = dsWSdev.ApplyUpload("AppUserDev", "pAs$4w0rd", "DocuShare", "property_name1", "property_value1", false, "AdminFolderCollection", file.FileName, data);
    
                        //Check Upload Result For Error
                        string strResult = xmlUploadResult.FirstChild.InnerText.ToString();
                        if (strResult != "200")
                        {
                            // Error Message
                            throw new ArgumentException("Error Uploading: " + xmlUploadResult.InnerText.ToString());
                        }
                        
    		    fStream.Flush();
                        br.Close();
                        fStream.Close();
    
                    }
                }
                return View();
            }

    Appreciate the help.

    Wednesday, May 9, 2018 7:46 PM
  • User303363814 posted

    My guess would be that there is a non-existing property column in the database.

    Thursday, May 10, 2018 7:30 AM
  • User-666661156 posted

    As per the error that's what it suggests, as far as my application isn't concerned, it doesn't share any database with the Webservice. So I think this would be on the webservice's end although I was just trying to make sure it's not code related. 

    Thursday, May 10, 2018 12:17 PM
  • User-666661156 posted

    PaulTheSmith

    My guess would be that there is a non-existing property column in the database.

    Turns out it was an issue with one of the values being passed from the application to the webservice, as it didn't recognize the actual variable for the column value, it's working fine now by using a blank value. Thanks for the help. 

    Thursday, May 10, 2018 2:11 PM