By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 814 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

.NET 2.0 C# Web Service File Upload Via HTML Form Post

P: n/a
I’m trying to upload a file to a Web Service. I have to submit the file
using a standard HTML form with the <input type=“file” /tag. Ultimately,
we are submitting the file from a Flash 8 application that uses Macromedia’s class. The FileReference class behaves like a
standard HTML form with the file input tag.

I know there are other options for submitting files through Web Services,
but we’re not able to write a customized .NET client.

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></webServicesin the web.config file.

I have a test HTML form, that POSTs the file and the filename to the web
service. However, I receive an error message:

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

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 = "")]
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;

The code for my HTML page is:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="" lang="en" xml:lang="en" >
<title>Test Upload</title>
<body >
<form id="UploadDocument"
action="" method="post"
<input type="file" id="docbinaryarray" name="docbinaryarray" />
<input type="text" id="docname" name="docname" />
<input type="submit" id="UploadMe" name="UploadMe" title="Upload File"
value="Upload File" />
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

John Wolff

Aug 15 '06 #1
Share this Question
Share on Google+
1 Reply

P: n/a
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
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 =
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
//Read in the file from the InputStream
Files[0].InputStream.Read(binaryWriteArray, 0,
//Open the file stream
FileStream objfilestream = new FileStream("c:\\" +
fileName, FileMode.Create, FileAccess.ReadWrite);
//Write the file and close it
objfilestream.Write(binaryWriteArray, 0,
return true;
return false;
catch (Exception ex1)
throw new Exception("Problem uploading file: " + ex1.Message);

Aug 15 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.