Hello all,
I am attempting to upload a file from a HTML page to my C# Web service using XMLHTTPRequest to post FormData to my Web Service that is expecting a byte array.
My other XMLHTTPRequest posts from my HTML Javascript work, however there is something I don't understand about posting files to C# Web Service using FormData.
My javascript XMLHTTPRequest reports state change to 0 and then Internal Service Error 500.
I've been trying for days and I ask out of desperation. Can anyone help?
Here are the important snippets. I'll add error trapping and different browser support once I can get the basics to work:
My HTML:
My Javascript:
My C# Web Service:
I am attempting to upload a file from a HTML page to my C# Web service using XMLHTTPRequest to post FormData to my Web Service that is expecting a byte array.
My other XMLHTTPRequest posts from my HTML Javascript work, however there is something I don't understand about posting files to C# Web Service using FormData.
My javascript XMLHTTPRequest reports state change to 0 and then Internal Service Error 500.
I've been trying for days and I ask out of desperation. Can anyone help?
Here are the important snippets. I'll add error trapping and different browser support once I can get the basics to work:
My HTML:
<input id="htmlinputfile" type="file" name="htmlinputfile" /><input type="button" value="Upload" onclick="uploadFile()" />
My Javascript:
function uploadFile() { var file = document.getElementById('htmlinputfile').files[0]; // Get the single file from the html input field. var formData = new FormData(); formData.append('file', file); // Append the file to the form data. var request = new XMLHttpRequest(); // I am only using firefox, so no need to worry about ie. request.onreadystatechange = function () { alert("status:" + request.status + " " + request.statusText); // Displays 0 and then the 500 Internal Service Error. if (request.readyState == 4 && request.status == 200) { alert("Upload successful!"); } } request.open("POST", 'http://localhost:59541/WebService.asmx/Upload', true); request.send(formData);
My C# Web Service:
using System; using System.Web.Services; using System.IO; namespace MouseEditor { [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] [System.Web.Script.Services.ScriptService] public class WebService : System.Web.Services.WebService { [WebMethod] public String Upload(byte[] bytes) { return "This is a test"; } } }