asp.net Web API file upload fileData empty

KellyB

New Member
I'm using any one of several examples found all over the place to do a file upload with .net Web API. The files get stored to the server, but the fileData object on the provider always returns empty. Code below.\[code\] var url = "api/Documents/fileUpload"; var xhr = new XMLHttpRequest(); var file = document.getElementById("inputFile").files[0]; var formData = http://stackoverflow.com/questions/15484619/new FormData(); formData.append('file', file); xhr.open("POST", url, true); xhr.responseType = "text"; xhr.onreadystatechange = function () { if (xhr.readyState == xhr.DONE) { console.log("photoUpload xhr", xhr); var responseTypeAsJSON = JSON.parse(xhr.responseText); currentPhoto.responseText = xhr.responseText; if (responseTypeAsJSON.result == "SUCCESS") { currentPhoto.status = "SUCCESSfully uploaded"; } else { currentPhoto.status = responseTypeAsJSON.result; alert(responseTypeAsJSON.message); } PhotoClear(); // console.log(currentPhoto); // console.log("xhr done: ", xhr); } } xhr.send(formData); // console.log("xhr sent: ", xhr);\[/code\]CONTROLLER TO RECEIVE:\[code\] [HttpPost] [ActionName("fileUpload")] public Task<HttpResponseMessage> fileUpload() { HttpRequestMessage request = this.Request; if (!request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } string root = System.Web.HttpContext.Current.Server.MapPath("~/App_Data"); var provider = new MultipartFormDataStreamProvider(root); var task = request.Content.ReadAsMultipartAsync(provider). ContinueWith<HttpResponseMessage>(o => { string file1 = provider.FileData.First().LocalFileName.ToString(); // this is the file name on the server where the file was saved return new HttpResponseMessage() { Content = new StringContent("File uploaded.") }; } ); return task; }\[/code\]
 
Back
Top