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
flash.net.FileReference 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 = "http://MyWebSpace.com/MyWebService/")]
public class MyWebService : WebService
{
[WebMethod]
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);
objfilestream.Close();
return true;
}
}
}
---------------
The code for my HTML page is:
---------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" >
<head>
<title>Test Upload</title>
</head>
<body >
<form id="UploadDocument"
action="http://MyWebSpace.com/MyWebService.asmx/UploadDocument" method="post"
enctype="multipart/form-data">
<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" />
</form>
</body>
</html>
---------------
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.
Thanks,
John Wolff
jdwolff@_NOSPAM_rocketmail.com