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
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

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
service.")]
public void GetFile(string fileName)
{
String filePath = "C:\\" + fileName;
HttpContext returnContext = HttpContext.Current;
returnContext.Response.TransmitFile(filePath);

}

[WebMethod(Description = "Upload a file from a POSTed web form.
Include the value - fileName ")]
public bool UploadFileCollection()
{
try
{
//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);
objfilestream.Close();
return true;
}
else
{
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.