Search code examples
c#iisbase64asmx

Best way of moving a file from one network to another c# iis


I am writing an IIS Web service (in an asmx file) using C# that takes a file as an input and moves it to a specific server directory.

What I am currently doing is, I convert this file into a Base64 string before calling the web service, and pass this Base64 type string into the web service. Web service converts the string into a file and saves it to the directory.

Is there a better practice ?


Solution

  • As L.B pointed out in the comments that;

    Web services (hosted in .asmx) use xml/soap based protocol which means every binary data has to be converted to text (base64, hex string etc. ) either manually or automatically.

    So the best way is what I was doing it from the beginning. Here is the codes that I use;

    Converting a file into Base64 type string;

    byte[] bytes = File.ReadAllBytes("file path");
    string file = Convert.ToBase64String(bytes);
    

    Converting Base64 string back into file;

    byte[] bytes = Convert.FromBase64String(b64Str);
    File.WriteAllBytes(path, bytes);