【发布时间】:2013-06-04 17:57:13
【问题描述】:
我正在尝试使用 ajax 提交表单。表单包含文本字段和文件上传字段。问题是它提交文本但不提交文件。我的表格是
<form onsubmit ="return save();" id="postadform" enctype="multipart/form-data">
Name<input type ="text" name ="name" />
Upload image <input type="file" name="image" id ="filee"/>
<input type ="submit" value = "submit" />
</form>
<script src="~/Scripts/jquery-1.7.1.js"></script>
<script type="text/javascript">
function save() {
$.ajax({
type: 'POST', url: '/Home/saveData', data: $('#postadform').serialize(),enctype:"multipart/form-data",
success: function (x) {
//do what ever you want
},
error: function () {
alert('There is some error, plz try again!');
}
});
return false;
}
</script>
这是 HomeController.cs 文件的一部分:
[HttpPost]
public String saveData()
{
String name = Request["name"];
String filename = Request.Files[0].FileName; //Problem in this line.
return "Successful";
}
【问题讨论】:
-
来自
serialize的 jQuery 文档:“来自文件选择元素的数据未序列化。” -
那么如何从文件字段中获取数据?
-
这已在 SO 上多次介绍过。例如:stackoverflow.com/questions/166221/…
标签: javascript jquery ajax file-upload