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.FileR eference 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><p rotocols><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.InvalidO perationExcepti on: Request format is invalid:
multipart/form-data; boundary=---------------------------7d6bb25507cc.
at System.Web.Serv ices.Protocols. HttpServerProto col.ReadParamet ers()
at System.Web.Serv ices.Protocols. WebServiceHandl er.CoreProcessR equest()
The code for my C# Web Service is:
---------------
using System;
using System.Web;
using System.Net;
using System.Web.Serv ices;
using System.IO;
namespace MyWebSpace.MyWe bService
{
[WebService(Name space = "http://MyWebSpace.com/MyWebService/")]
public class MyWebService : WebService
{
[WebMethod]
public bool UploadDocument( byte[] docbinaryarray, string docname)
{
string strdocPath;
strdocPath = "C:\\DocumentDi rectory\\" + docname;
FileStream objfilestream = new FileStream(strd ocPath,
FileMode.Create , FileAccess.Read Write);
objfilestream.W rite(docbinarya rray, 0, docbinaryarray. Length);
objfilestream.C lose();
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.dt d">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" >
<head>
<title>Test Upload</title>
</head>
<body >
<form id="UploadDocum ent"
action="http://MyWebSpace.com/MyWebService.as mx/UploadDocument" method="post"
enctype="multip art/form-data">
<input type="file" id="docbinaryar ray" name="docbinary array" />
<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