以下是 cshtml 页面的代码:
@using (Ajax.BeginForm("QuotePost", "Quote", null, new AjaxOptions()
{
UpdateTargetId = "response",
OnSuccess = "OnSuccessLogin",
OnFailure = "onFailLogin",
HttpMethod = "Post"
}, new { id = "frmQuote", enctype = "multipart/form-data" }))
{
<div class="col-xs-3">
<input name="files" type="file" id="fileupload1" />
</div>
<div class="col-xs-3">
<input type="submit" id="Upload1" value="Upload" class="btn btn-default" />
</div>
}
在我的控制器中
public JsonResult UploadQuoteByIMEI(HttpPostedFileBase PostedFile)
{
try
{
if (PostedFile.ContentLength > 0)
{
var fileName = Path.GetFileName(PostedFile.FileName);
var path = Path.Combine(Server.MapPath("~/Images/"), fileName);
PostedFile.SaveAs(path);
}
return Json(null, JsonRequestBehavior.AllowGet);
}
catch(Exception ex)
{
throw ex;
}
}
在我的 JS
中$(document).on('click', '#Upload1', function (e) {
$('#frmQuote').submit();
$.ajax({
url: "Quote/UploadQuoteByIMEI",
type: "POST",
dataType: "json",
success: function (data) {
}
});
});
注意:在我的控制器中,我已将null返回到json中,但我担心的是,作为HttpPostedFileBase 的 PostedFile传入的参数为空。
请帮助我。
我已关注的链接 http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx/