【问题标题】:File is not getting saved文件未保存
【发布时间】:2013-03-06 08:55:18
【问题描述】:

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

【问题讨论】:

  • 在这种情况下,如果我使用“HttpPostedFileBase”类会出错..即文件=空

标签: asp.net-mvc


【解决方案1】:

你没有保存它。关于如何保存文件,请参阅下面的帖子:

File upload in MVC

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

【讨论】:

    【解决方案2】:

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

    “HttpPostedFileBase”类。

    Here's the sample Code

    【讨论】:

      【解决方案3】:

      使用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 帖子更改为 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>
      

      【讨论】:

      • 如果我使用 HttpPostedFileBase uploadFile 我得到一个错误,即 uploadFile=null
      【解决方案4】:

      首先你需要告诉表单的 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
      }
      

      【讨论】:

        猜你喜欢
        • 2020-07-29
        • 2021-11-23
        • 2023-03-15
        • 1970-01-01
        • 2018-09-29
        • 2015-01-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多