I am trying to implement Gmail style drag-and-drop file upload in ASP.NET MVC.
I have been following this article: http://robertnyman.com/html5/fileapi-upload/fileapi-upload.html and want to post uploaded files to an MVC controller action.
To do this, I modified the sample JavaScript script in the link to point to my controller action:
xhr.open("post", "/home/UploadFiles", true);
Here is my controller action:
[HttpPost]
public virtual string UploadFiles(object obj)
{
var length = Request.ContentLength;
var bytes = new byte[length];
Request.InputStream.Read(bytes, 0, length);
// var bytes has byte content here. what do do next?
return "Files uploaded!";
}
I set a breakpoint, and when I upload a file, the breakpoint gets hit - which is good. But how do I extract the data from the uploaded (javascript) XMLHttpRequest object? I don't think its in the HttpRequest - is it the parameter? If so, what type should i expect & how do I extract the byte array and extract the uploaded file info from it?
(I am using Chrome - I know it doesn't work in IE)
Any suggestions would be greatly appreciated!
Figured it out. Here is the C# code:
And here's the Javascript code:
This is great. Improved slightly: