在asp.net mvc中使用jQuery ajax上传文件我的视图中有一个文件<form id="upload" enctype="multipart/form-data">
<input type="file" name="fileUpload" id="fileUpload" size="23" /></form>和ajax请求$.ajax({
url: '<%=Url.Action("JsonSave","Survey") %>',
dataType: 'json',
processData: false,
contentType: "multipart/mixed",
data: {
Id: selectedRow.Id,
Value: 'some date was added by the user here :))'
},
cache: false,
success: function (data) {}});但Request.Files中没有文件。ajax请求有什么问题?
3 回答
qq_笑_17
TA贡献1818条经验 获得超7个赞
现在可以通过将FormData
对象传递给请求的data
属性来上传AJAX文件$.ajax
。
由于OP专门要求jQuery实现,所以你去:
<form id="upload" enctype="multipart/form-data" action="@Url.Action("JsonSave", "Survey")" method="POST"> <input type="file" name="fileUpload" id="fileUpload" size="23" /><br /> <button>Upload!</button></form>
$('#upload').submit(function(e) { e.preventDefault(); // stop the standard form submission $.ajax({ url: this.action, type: this.method, data: new FormData(this), cache: false, contentType: false, processData: false, success: function (data) { console.log(data.UploadedFileCount + ' file(s) uploaded successfully'); }, error: function(xhr, error, status) { console.log(error, status); } });});
public JsonResult Survey(){ for (int i = 0; i < Request.Files.Count; i++) { var file = Request.Files[i]; // save file as required here... } return Json(new { UploadedFileCount = Request.Files.Count });}
- 3 回答
- 0 关注
- 1383 浏览
添加回答
举报
0/150
提交
取消