2 Replies Latest reply on Sep 25, 2006 11:31 PM by Deniom

    .NET 2.0 C# Web Service File Upload Via FileReference

      I’m trying to upload a file to a Web Service. Ultimately, we are submitting the file from a Flash 8 application that uses Macromedia’s flash.net.FileReference class.

      I’ve written a web service in C# on the .NET 2.0 Framework using Visual Studio 2005. It is configured to accept the binary data of the file, and a file name as a string. It then writes the file to the web server’s (IIS 6 on Windows 2003) file system. I’ve configured my web service to accept http posts using <webServices><protocols><add name="HttpPost"/></protocols></webServices> in the web.config file.

      I have a test Flash form, that POSTs the file and the filename to the web service. The form works fine when I post to a PHP file, but we're migrating off PHP and to a web service platform using .NET. When I submit the file to the web serivce, I get an error message:

      System.InvalidOperationException: Request format is invalid: multipart/form-data; boundary=---------------------------7d6bb25507cc.
      at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
      at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

      The code for my C# Web Service is:
      using System;
      using System.Web;
      using System.Net;
      using System.Web.Services;
      using System.IO;

      namespace MyWebSpace.MyWebService
      [WebService(Namespace = " http://MyWebSpace.com/MyWebService/")
      public class MyWebService : WebService
      public bool UploadDocument(byte[] docbinaryarray, string docname)
      string strdocPath;
      strdocPath = "C:\\DocumentDirectory\\" + docname;
      FileStream objfilestream = new FileStream(strdocPath, FileMode.Create, FileAccess.ReadWrite);
      objfilestream.Write(docbinaryarray, 0, docbinaryarray.Length);
      return true;

      Any idea what’s going wrong? Is there a better way to write a Web Service that accept file posts from an HTML form? Any help would be greatly appreciated.

      John Wolff

        • 1. Re: .NET 2.0 C# Web Service File Upload Via FileReference
          JohnWolff Level 1
          Problem resolved! The problem was reading in the binary data as a parameter input. I needed to be reading the HttpContext input stream.

          Here's my code that I used on the WebService side.

          [WebMethod(Description = "Download a file that was POSTed to the web service.")]
          public void GetFile(string fileName)
          String filePath = "C:\\" + fileName;
          HttpContext returnContext = HttpContext.Current;


          [WebMethod(Description = "Upload a file from a POSTed web form. Include the value - fileName ")]
          public bool UploadFileCollection()
          //HTTP Context to get access to the submitted data
          HttpContext postedContext = HttpContext.Current;
          //File Collection that was submitted with posted data
          HttpFileCollection Files = postedContext.Request.Files;
          //Make sure a file was posted
          string fileName = (string)postedContext.Request.Form["fileName"];
          if (Files.Count == 1 && Files[0].ContentLength > 1 && fileName != null && fileName != "")
          //The byte array we'll use to write the file with
          byte[] binaryWriteArray = new byte[Files[0].InputStream.Length];
          //Read in the file from the InputStream
          Files[0].InputStream.Read(binaryWriteArray, 0, (int)Files[0].InputStream.Length);
          //Open the file stream
          FileStream objfilestream = new FileStream("c:\\" + fileName, FileMode.Create, FileAccess.ReadWrite);
          //Write the file and close it
          objfilestream.Write(binaryWriteArray, 0, binaryWriteArray.Length);
          return true;
          return false;
          catch (Exception ex1)
          throw new Exception("Problem uploading file: " + ex1.Message);
          • 2. Re: .NET 2.0 C# Web Service File Upload Via FileReference
            Thank you for posting your code :-)