0

在我的 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();
        }
    }
4

4 回答 4

1

你没有保存它。只需查看以下帖子了解如何保存文件:

MVC 中的文件上传

完整教程:http ://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx

于 2013-03-18T04:31:15.400 回答
0

使用HttpPostedFileBase uploadFile参数接受上传文件并SaveAs(filePath);保存!

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult FileUpload(HttpPostedFileBase uploadFile)
{
    if (uploadFile.ContentLength > 0)
    {
        string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"), 
                                       Path.GetFileName(uploadFile.FileName));
        uploadFile.SaveAs(filePath);
    }
    return View();
}

还将您的 JQuery Post 更改为 Jquery Ajax 帖子

$('form').submit(function(event) {
                    event.preventDefault();
                    var file = $("#filename").val();
                    file = file.serialize();
                    $.ajax({  
                        type: "POST",
                        contentType:attr( "enctype", "multipart/form-data" ),
                        url: "/FileUpload/UploadURL",  
                        data: file,  
                        success: function( data )  
                        {  
                            alert( data );  
                        }  
                    });  

                    return false;  
                }  
 </script>
于 2013-03-18T04:36:42.923 回答
0

首先,您需要告诉表单的 EncType。没有 encType 文件将不会发布到服务器

<form action="" method="post" enctype="multipart/form-data">
</form>

用于剃须刀

@using (Html.BeginForm("Index", "yourCOntroller", FormMethod.POST, new { enctype = "multipart/form-data" }))
{
  // some stuff
}
于 2013-03-18T05:06:15.107 回答
0

实际上在代码中的哪个位置保存文件???尝试利用

“HttpPostedFileBase”类。

这是示例代码

于 2013-03-18T04:41:48.607 回答