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
});
});
$.ajaxSettings.xhr()is and how it sends the file to server. You seem to be testing against someuploadproperty on it. Looks like some custom built stuff. Just use FireBug to see what encoding is used and how the file is transmitted over the wire. If it usesmultipart/form-datathen its trivial to retrieve it.multipart/form-dataencoded content you could simply use anHttpPostedFileBaseargument in your controller action which will contain the uploaded file. If your client code uses some other non-standard protocol for uploading the file to the server I am afraid that we cannot know how to answer your question without knowing how the file is transmitted. At the worst case if you are using some custom stuff you could access the raw InputStream of the request.