通过这样的投递文件,mvc4的控制器操作应该是什么样的?
<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
});
});发布于 2013-07-19 21:40:26
看起来,上传文件的正常MVC操作将起作用:
[HttpPost]
public ActionResult fileupload(HttpPostedFileBase file)
{
// do something with file.InputStream
}https://stackoverflow.com/questions/17755810
复制相似问题