Reputation: 4862
how the controller action of mvc4 should look like by posting file like this ?
Html
<form enctype="multipart/form-data">
<input name="file" type="file" />
<input type="button" value="Upload" />
</form>
Javascript
$(':button').click(function(){
var formData = new FormData($('form')[0]);
$.ajax({
url: 'api/fileupload', //server script to process data
type: 'POST',
xhr: function() { // custom xhr
var myXhr = $.ajaxSettings.xhr();
if(myXhr.upload){ // check if upload property exists
myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // for handling the progress of the upload
}
return myXhr;
},
//Ajax events
beforeSend: beforeSendHandler,
success: completeHandler,
error: errorHandler,
// Form data
data: formData,
//Options to tell JQuery not to process data or worry about content-type
cache: false,
contentType: false,
processData: false
});
});
Upvotes: 2
Views: 1845
Reputation: 40393
Looks like the normal MVC action for uploading a file will work:
[HttpPost]
public ActionResult fileupload(HttpPostedFileBase file)
{
// do something with file.InputStream
}
Upvotes: 4