在我的 MVC 应用程序中,我允许用户上传 PDF 文件,上传的文件保存在文件夹中。该文件已正确上传,但未保存在文件夹中...查看代码为:
<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 save_file(box) {
var filename = $("#filename").val();
if (filename == "") {
alert("Enter the URL");
return false;
}
dhtmlx.modalbox.hide(box);
dhtmlx.message("Uploading the file");
$.post("/FileUpload/UploadURL",
{ filename: '' + filename + '' });
}
控制器代码为:
public ActionResult UploadURL(FormCollection data)
{
var filename=data["filename"];
SaveNewFile(filename);
return View();
}
public ActionResult SaveNewFile(string file)
{
var supportedType = new[] { "pdf" };
var fileExt = System.IO.Path.GetExtension(file).Substring(1);
var filename = Path.GetFileNameWithoutExtension(file) ?? "";
if (file.Length > 0 && supportedType.Contains(fileExt))
{
string filePath = Path.Combine(HttpContext.Server.MapPath(_fileUploadPath),
Path.GetFileName(file));
if (!System.IO.File.Exists(filePath))
{
filePath = Server.MapPath(_fileUploadPath + file);
TempData["UploadValidationMessage_Success"] = "File upload Succeeded.";
return View();
}
else
{
TempData["UploadValidationMessage_Failure"] = "File already exist.";
return View();
}
}
else
{
TempData["UploadValidationMessage_Failure"] = "Only PDF files are supported. Try again...";
return View();
}
}