我正在使用 asp.net MVC3
我想在提交表单之前上传图片。
下面是我的代码
<div>
@using (Html.BeginForm("SaveData", "Home", FormMethod.Post, new { @enctype = "multipart/form-data" }))
{
<div> Image:
<input type="file" name="uploadFile" onchange="upload(this);" /></div>
<div> Name:
@Html.TextBoxFor(model => model.Name)</div>
<div> Address:
@Html.TextBoxFor(model => model.Address)</div>
<div> Phone:
@Html.TextBoxFor(model => model.Phone)</div>
<input type="submit" name="save" id="savedata" title="save"
value="save" />
}
</div>
<script type="text/javascript">
function UploadFile(image) {
//call controller action UploadFile
$.ajax({
type: "Post",
url: "Home/UploadFile",
data: image, //This is wrong
contentType: "application/json; charset=utf-8",
dataType: "json"
});
}
</script>
现在在控制器端
public ActionResult UploadFile(HttpPostedFileBase uploadFile)
{
//save image
}
任何人都可以建议我如何在发布表单以保存数据之前上传图像。