2

通过像这样发布文件,mvc4 的控制器动作应该如何?

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
    });
});
4

1 回答 1

4

看起来上传文件的正常 MVC 操作将起作用:

[HttpPost]
public ActionResult fileupload(HttpPostedFileBase file)
{
    // do something with file.InputStream
}
于 2013-07-19T21:40:26.870 回答