473,765 Members | 2,172 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Aug 15 '06 #1
1 31533
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(Descr iption = "Download a file that was POSTed to the web
service.")]
public void GetFile(string fileName)
{
String filePath = "C:\\" + fileName;
HttpContext returnContext = HttpContext.Cur rent;
returnContext.R esponse.Transmi tFile(filePath) ;

}

[WebMethod(Descr iption = "Upload a file from a POSTed web form.
Include the value - fileName ")]
public bool UploadFileColle ction()
{
try
{
//HTTP Context to get access to the submitted data
HttpContext postedContext = HttpContext.Cur rent;
//File Collection that was submitted with posted data
HttpFileCollect ion Files = postedContext.R equest.Files;
//Make sure a file was posted
string fileName =
(string)postedC ontext.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[] binaryWriteArra y = new
byte[Files[0].InputStream.Le ngth];
//Read in the file from the InputStream
Files[0].InputStream.Re ad(binaryWriteA rray, 0,
(int)Files[0].InputStream.Le ngth);
//Open the file stream
FileStream objfilestream = new FileStream("c:\ \" +
fileName, FileMode.Create , FileAccess.Read Write);
//Write the file and close it
objfilestream.W rite(binaryWrit eArray, 0,
binaryWriteArra y.Length);
objfilestream.C lose();
return true;
}
else
{
return false;
}
}
catch (Exception ex1)
{
throw new Exception("Prob lem 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
9223
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 plain text file at the server side. Example: 1) My form will have two fields: i) Name: myName ii) Age: 33
2
3576
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 type=submit value=OK /> </form></body></html>
1
1324
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
1374
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 dialog. How can I post a file using asp.net? the form allows for some of the above options, but as I'm using master pages I don't want this change to
0
1754
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"); @input=<FH1>; print(@input); @input=($login,$password); close(FH1);
9
2338
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 experienced with SOAP/WSDL and I believe that would be more difficult than necessary. The client program I have to use does not support FTP.
3
4291
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 ... request.open("GET", "do.asp?"+Date.parse(Date()), true); ...do.asp file on server side should respond to abc and do sth. do.asp
0
4694
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", "dialogWidth:500px;dialogHeight:400px"); } else { window.open('MyPage1.asp,'MyPopup',
4
11438
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 dynamically from a .txt file. I was wondering if how to make a form with a text area that will populate automatically from what is currently in the txt file. Then I could edit the data from there, submit it and it will all be updated. I realize all of...
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9951
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9832
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8831
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, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.