By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,187 Members | 1,038 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.

post form data to web service

P: n/a
I am trying to receive form data posted from an html page to a web
service method. In other words, a web page posts data as
encrypt=multipart/form-data, the action is the webservice method, i.e.
action=webservice/methodname. The form includes the contents of an uploaded
file, i.e. a file browse button in the form.
I can perform similar actions taking in just string data and post it to
the web service and process the data. But I seem to be having a problem with
the uploaded file from the form.
Any suggestions on how to do this, or how to receive the data? I can not
modify the original html page that has the form, other than the url in the
action of the form.

Thanks,
J
Nov 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
I figured it out, thought I would post some sample code in case anyone
else is looking:
FYI: The code needs work before being "production" ready. I do not
believe this will work for some types of files, such as images, because it
would need to be changed to a base64 binary read. But I do not have this
need, I needed to read in standard text files.

[WebMethod]
public string GetData()
{
string theReturn = "";
StringBuilder sbOutput = new StringBuilder();
try
{
// the constructor for the http context
HttpContext theContext = HttpContext.Current;
// file collection of uploaded files in the http context
HttpFileCollection Files = theContext.Request.Files;
// if something there then read the posted upload
if(1==Files.Count & 1<Files[0].ContentLength)
{
theReturn += Files[0].ContentLength;
theReturn += Files[0].ContentType;
using(StreamReader srFiledata = new StreamReader(Files[0].InputStream))
{
sbOutput.Append(srFiledata.ReadToEnd());
}
// double check to make sure this is valid upload
// to do code
theReturn+=sbOutput.ToString();
// now pick up the additional form fields

theReturn += theContext.Request.Form["dbConnection"];
//theReturn += theContext.Request.Form[2];
}
// here send back something to indicate that nothing was uploaded or it
is invalid
else
{
theReturn += "Nothing Uploaded";
}
}
catch(Exception ex1)
{
theReturn = ex1.Message;
}

return theReturn;
}

"Scanner2001" <je************@hotmail.com> wrote in message
news:O1**************@TK2MSFTNGP09.phx.gbl...
I am trying to receive form data posted from an html page to a web
service method. In other words, a web page posts data as
encrypt=multipart/form-data, the action is the webservice method, i.e.
action=webservice/methodname. The form includes the contents of an uploaded file, i.e. a file browse button in the form.
I can perform similar actions taking in just string data and post it to the web service and process the data. But I seem to be having a problem with the uploaded file from the form.
Any suggestions on how to do this, or how to receive the data? I can not modify the original html page that has the form, other than the url in the
action of the form.

Thanks,
J

Nov 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.