私はすでに mvc3 で通常どおりファイルを送信していました。今度は Ajax で同じことを行う必要があります。そのため、この jquery プラグインを使用しました :
コードを表示:
$(document).ready(function () {
var options = {
url: "/Home/TakeFile",
dataType: "json",
success: showResponse
};
$("#File").submit(function () {
alert("submit");
$(this).ajaxSubmit(options);
return false;
});
});
function showResponse(responseText, statusText, xhr, $form) {
alert("showResponse");
alert(responseText.fileName);
}
</script>
@using (Html.BeginForm("TakeFile", "Home", FormMethod.Post, new { @id = "File", enctype = "multipart/form-data" }))
{
<input type="file" id="file" />
<input type="submit" value="Click to submit" id="button" />
}
コントローラーコード:
[HttpPost]
public ActionResult TakeFile(HttpPostedFileBase file)
{
return Json(new { fileName=file.FileName});
}
「TakeFile」メソッドのファイル パラメータは常に null です。機能していないようです。また、「Ajax.BeginForm()」ヘルパーを使用して実行できますか??