473,408 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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

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
1 31489
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jamil | last post by:
I am a novice in HTML. I just downloaded Apache server on my Windows 2000 desktop. It is running properly. I need to write a form in HTML which will save the input data enterd by the user in a...
2
by: Sergey Poberezovskiy | last post by:
Hi, I am trying to read form values from HTML form in ASP.Net. For example: first.html file <html><body><form action="second.aspx" method="post"> <input type=text id="txtOne" /> <input...
1
by: MS | last post by:
HI, is there any alternate of THML file upload control...? can we get stream from the fileupload control to save it temporarly in session and then later on saving to some directory...
4
by: Not Me | last post by:
Hi, I'm trying to translate a page into asp.net 2.0.. The options I need are e.g. action='some webpage', method=post, enctype='enctype' etc... the file is supplied by a standard file input...
0
by: supern | last post by:
#!c:/perl/bin/perl.exe $basedir="c:/program files/apache software foundation/apache2.2/cgi-bin"; $datafile="regstr.txt"; $name=$in{'login'}; $passwd=$in{'passwd'}; open(FH1,"+>>regstr.txt");...
9
by: Karsten.G.Weinert | last post by:
Hello, what is the simplest way to upload a file (or a long string) to a server using cgi/python? Since I want to upload the data programmatically, a form based solution is not good. I am not...
3
by: 1965 | last post by:
Hi, All. I want to pass a value to asp file on server side but NOT using form post/get. For example: <input id="regionbox" type="hidden" name="region" value="abc"> function dothing ......
0
by: magix | last post by:
Hi, I launched a modal popup window with code like: <script language="javascript"> function modalWin() { if (window.showModalDialog) { window.showModalDialog("MyPage1.asp","MyPopup",...
4
by: mbatestblrock | last post by:
Hey guys, I assume there is a way to do this and it shouldnt be too difficult. I am just having a heck of time finding out how to do exactly this... I have a flash file that is pulling text...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.