ASP.NET Save file in a folder

0

Hello friends I am trying to save a file inside a folder that I create dynamically, up to here there is no problem, the point is that when I try to pass the file already in my method I receive it Like Null , could someone tell me where I'm missing?  I'm getting this error.

    [HttpPost]        
    public void  CreateFolder() {

        if (Request.Files.Count > 0)
        {

            foreach (HttpPostedFile file in Request.Files)
            {

                Random rnd = new Random();

                int rndx = rnd.Next(0, 1000);

                string extension = System.IO.Path.GetExtension(file.FileName);
                string fname = System.IO.Path.GetFileName(file.FileName);
                string dt = DateTime.Now.ToString("M/d/yyyy");
                
                
                var folder = Server.MapPath("~/Facturas/" + rndx);
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                    string filePath = Path.Combine(Server.MapPath("~/Facturas/" + rndx)
                    , "" + rndx + "_" + dt + "_" + fname + "_" + extension);
                    file.SaveAs(filePath);
                }
            }

       

        }
    //end method
    }
@using (Html.BeginForm("CreateFolder", "SAMAcceso", FormMethod.Post)) { @Html.TextBox("file","",new {type= "file" })
<input type="submit" value="cargar" /> }
    
asked by E.Rawrdríguez.Ophanim 16.05.2018 в 21:04
source

2 answers

0

Well, this I ended up doing, in case someone serves you

[HttpPost]
public void CreateFolder() {

  if (Request.Files.Count > 0) {

    var file = Request.Files[0];
    Random rnd = new Random();

    int rndx = rnd.Next(0, 1000);

    string extension = System.IO.Path.GetExtension(file.FileName);
    string fname = System.IO.Path.GetFileName(file.FileName);
    string dt = DateTime.Now.ToString("M/d/yyyy");

    var folder = Server.MapPath("~/Facturas/" + rndx);
    if (!Directory.Exists(folder)) {
      Directory.CreateDirectory(folder);
      string filePath = Path.Combine(Server.MapPath("~/Facturas/" + rndx), rndx + "_" + "_" + "_" + extension);
      file.SaveAs(filePath);
    }




  }
  //end method
}
    
answered by 17.05.2018 / 16:47
source
1

In the form you must add the multipart/form-data attribute as follows

@using (Html.BeginForm("CreateFolder", "SAMAcceso", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.TextBox("file", "", new { type = "file" })
    <input type="submit" value="cargar" /> 
}

The HttpPostedFile file parameter should be removed and the file obtained as follows:

public void CreateFolder()
{
   if (Request.Files.Count > 0)
   {
      foreach(string filename in Request.Files)
      {
         HttpPostedFileBase file = Request.Files[filename];
      }
   }
}

The foreach would be used exclusively if your method will receive multiple files , otherwise a Request.Files[0] would suffice.

With that it would be enough, we commented that such

    
answered by 16.05.2018 в 22:01