【发布时间】:2016-01-20 12:26:09
【问题描述】:
我正在尝试使用 jQuery 将文件发送到我的 MVC 控制器,但该操作一直收到一个空的 HttpPostedFileBase 参数。
HTML:
<input type="file" name="file" id="file" />
<input type="submit" name="submit" id="upload" value="Submit"/>
jQuery:
$(function () {
$('#upload').click(function () {
var data = new FormData($('#file')[0].files[0]);
$.ajax({
url: '@Url.Action("Upload", "Home")',
type: 'POST',
data: data,
cache: false,
contentType: false,
processData: false
});
});
});
控制器:
[HttpPost]
public virtual ActionResult Upload(HttpPostedFileBase file)
{
// file = null
}
新的FormData($('#file')[0].files[0]):
__proto__: FormData
$('#file')[0].files[0]:
lastModified: 1445429215528
lastModifiedDate: Wed Oct 21 2015 14:06:55 GMT+0200 (Central Europe Daylight Time)
name: "Google_Chrome_logo_2011.jpg"
size: 5506
type: "image/jpg"
webkitRelativePath: ""
__proto__: File
我几乎从互联网上找到的其他示例中复制了代码,但不知何故它不起作用。
【问题讨论】:
-
您可以从 this question and answer mentioned here 获得一些帮助
标签: javascript jquery ajax asp.net-mvc