【问题标题】:Form Collection is empty When I use enctype for file uploading当我使用 enctype 进行文件上传时,表单集合为空
【发布时间】:2012-03-18 09:21:54
【问题描述】:

我正在 asp.net 中开发一个应用程序,我将在其中上传文件,但是当我使用 enctype = "multipart/form-data" 表单集合为空并且当我不使用 enctype 时,表单集合的名称为上传文件但 Request.Files.count = 0。我想获取文件上传以及表单集合中上传文件的名称。有什么解决办法吗?

【问题讨论】:

    标签: c# asp.net asp.net-mvc


    【解决方案1】:

    查看以下代码:

    通过以下代码以视图形式添加编码类型:

    @using (Html.BeginForm("Create", "Employees", FormMethod.Post,new{ enctype="multipart/form-data"}))
    {   
         @Html.TextBoxFor(model => model.Name)
    
         @Html.TextBoxFor(model => model.Resume, new { type = "file" })
    
        <p>
        <input type="submit" value="Save" />
        </p>
    @Html.ValidationSummary()
    }
    

    在控制器的相应操作中添加以下代码,

    [HttpPost]
    public ActionResult Create(EmployeeViewModel viewModel)
    {
           if (Request.Files.Count > 0)
            {
                foreach (string file in Request.Files)
                {
                    string pathFile = string.Empty;
                    if (file != null)
                    {
                        string path = string.Empty;
                        string fileName = string.Empty;
                        string fullPath = string.Empty;
                        path = AppDomain.CurrentDomain.BaseDirectory + "directory where you want to upload file";//here give the directory where you want to save your file
                        if (!System.IO.Directory.Exists(path))//if path do not exit
                        {
                            System.IO.Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "directory_name/");//if given directory dont exist, it creates with give directory name
                        }
                        fileName = Request.Files[file].FileName;
    
                        fullPath = Path.Combine(path, fileName);
                        if (!System.IO.File.Exists(fullPath))
                        {
    
                            if (fileName != null && fileName.Trim().Length > 0)
                            {
                                Request.Files[file].SaveAs(fullPath);
                            }
                        }
                    }
                }
            }
    }
    

    我假设路径将在 basedirectory 的目录内....您可以提供自己想要保存文件的路径

    【讨论】:

      【解决方案2】:

      以下对我来说很好用:

      @using (Html.BeginForm("someaction", "somecontroller", FormMethod.Post, new { enctype = "multipart/form-data" }))
      {
          <input type="file" name="file" />
          <button type="submit">Upload</button>
      }
      

      请注意,文件输入必须有一个name,稍后将在控制器操作中使用它来获取文件。您得到 Request.File.Count = 0 的事实非常强烈地表明您没有为输入字段提供名称。

      和行动:

      [HttpPost]
      public ActionResult SomeAction(HttpPostedFileBase file)
      {
          if (file != null && file.ContentLength > 0)
          {
              var filename = Path.GetFileName(file.FileName);
              filename = Path.Combine(Server.MapPath("~/uploads"), filename);
              file.SaveAs(filename);
          }
          return View();
      }
      

      如果您想使用 FormCollection(我不推荐):

      [HttpPost]
      public ActionResult SomeAction()
      {
          var file = Request.Files["file"];
          if (file != null && file.ContentLength > 0)
          {
              var filename = Path.GetFileName(file.FileName);
              filename = Path.Combine(Server.MapPath("~/uploads"), filename);
              file.SaveAs(filename);
          }
          return View();
      }
      

      您也可以查看following blog post

      【讨论】:

        【解决方案3】:

        要怎么用:

        @Microsoft.Web.Helpers.FileUpload.GetHtml("File", 1, false, false, null, null)
        

        然后在你的控制器中:

        public ActionResult YourAction()
        {
            Request.Files[0]; //Check this out !
        }
        

        更多信息:

        StackOverFlow : https://stackoverflow.com/a/5060318

        Tallan 博客:http://blog.tallan.com/2011/02/04/using-mvc3-razor-helpers-and-jcrop-to-upload-and-crop-images/

        【讨论】:

          猜你喜欢
          • 2012-03-03
          • 1970-01-01
          • 2013-02-03
          • 1970-01-01
          • 1970-01-01
          • 2019-09-09
          • 1970-01-01
          • 2021-06-02
          • 2021-05-01
          相关资源
          最近更新 更多