在我的 MVC 应用程序中,我允许用户上传文件。每当用户单击上传文件链接时,这就是链接
<a class="upload" onclick="upload(this);">
文件上传应该在模式框中打开。
function upload(box) {
var box = dhtmlx.modalbox({
title: "Upload File",
text: "<div id='form_in_box'><div>Choose a PDF file to upload <hr/><label>Enter the URL <input type='file' name='file' id='file' style='width: 400px; height: 27px;'></label><br></div><div><span class='dhtmlx_button'><input type='submit' value='Upload File' style='width: 86px' onclick='save_file(this)'></span><span class='dhtmlx_button'><input type='button' value='Cancel' onclick='close_file(this)' style='width:80px;'></span></label></div></div>",
width: "300px"
});
}
function close_file(box) {
dhtmlx.modalbox.hide(box);
}
function save_file(box) {
var file = $("#file").val();
if (file == "") {
alert("Enter the URL");
return false;
dhtmlx.modalbox.hide(box);
dhtmlx.message("Uploading the file");
$.post("/FileUpload/Upload",
{ file: '' + file + '' });
}
and the controller code is
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
SaveFile(file);
return RedirectToAction("Index");
}
但问题是出现错误,即 file = null