0
您好我有一個對話框彈出包含更新的形式和 我創建的更新動作,我加入了一些上傳代碼 問題是輸入文件的形式始終空上傳輸入爲什麼HttpPostedFileBase文件總是空
控制器
[HttpPost]
public ActionResult Setting(user user, HttpPostedFileBase file)
{
查看
@model Magellane.Models.user
@using (Html.BeginForm("Setting", "Account", FormMethod.Post, new { role = "form", id = "profileForm", @class = "form-horizontal", enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<input type="hidden" name="user_id" value="@Session["user_id"]">
<div class="form-group">
<label for="pass1" class="col-sm-2 control-label">Nom decole</label>
<div class="col-sm-10">
<input class="form-control-modal required" id="company" type="text" value="@Model.company" name="company" minlength=5>
<span class="field-validation-error" data-valmsg-for="company" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<label for="pass1" class="col-sm-2 control-label">Logo</label>
<input type="file" class="margin-none" name="logo" id="file" onchange="readURL(this);" />
</div>
$.ajax({
url: '/Account/setting',
type: 'POST',
data: $('form').serialize(),
dataType: 'json',
async: false,
success: function (xhr, status, error) {
bootbox.alert(xhr.message);
},
error: function (xhr, ajaxOptions, thrownError) {
bootbox.alert(thrownError);
}
});
有沒有什麼解決辦法嗎?
爲了使用ajax發佈文件,您需要使用FormData並設置正確的ajax選項。參考[這個答案](http://stackoverflow.com/questions/29293637/how-to-append-whole-set-of-model-to-formdata-and-obtain-it-in-mvc/29293681#29293681)例如 –
檢查此鏈接以獲取傳輸文件的完整代碼[鏈接](http://stackoverflow.com/questions/40644898/mvc5-ajax-beginform-upload-form-with-files/40648538#40648538) –