【发布时间】:2013-01-29 19:41:12
【问题描述】:
我进行了深入研究,试图找到从客户端上传文件的更实用的方法,我决定使用输入类型文件 html 元素。
问题是: 我在控制器中什么都没有,我什至看不到在 firebug 中设置了 fakepath 字符串。
查看:
<form action="action" id="id" method="POST" enctype="multipart/form-data">
<table id="tblId">
<tr>
<td><input type="file" name="file" /></td>
</tr>
</table>
<input type="submit" value="import" />
</form>
如您所见,我正确使用了 enctype。数据已按应有的方式发送到控制器,但没有数据。
控制器:
[HttpPost]
public ActionResult opImportFile(FormCollection form) {
var file = Request.Files["file"];
if (file != null)
{
return Content("ok");
}
else
{
return Content("bad");
}
}
而且我总是变得“坏”!太糟糕了。 有没有其他方法可以尝试或者我做错了什么?
P.S -> 这是一个ajax请求:
$('#formUpdStoringSettings').submit(function (e) {
e.preventDefault();
var form = $(this);
alert($('input[name=file]').val()); //Here I am able to get the fakepath...
alert(form.serialize()); //Here I get nothing...
$.ajax({
url: form.attr('action'),
type: form.attr('method'),
data: form.serialize(),
success: function (response) {
}
});
}
【问题讨论】:
-
你见过这个吗?您可能需要使用 FormData() stackoverflow.com/questions/2320069/…
-
您可能需要使用 jQuery 插件才能使用 AJAX 进行上传。 blueimp.github.com/jQuery-File-Upload
-
谢谢... ajax 是问题
-
@QuetiMporta Anwser 这个问题,所以我可以为您提供一些声誉。
标签: c# asp.net .net asp.net-mvc asp.net-mvc-4