39

I am trying to upload files using aspnet core using ajax request . In previous versions of .net i used to handle this using

 foreach (string fileName in Request.Files)
            {
                HttpPostedFileBase file = Request.Files[fileName];
                //Save file content goes here

                fName = file.FileName;
     (...)

but now its showing error at request.files how can i get it to work ? i searched and found that httppostedfile has been changed to iformfile but how to handle request.files?

Ironsun
  • 801
  • 2
  • 8
  • 19
  • 2
    take a look at these links http://stackoverflow.com/questions/26443305/how-can-i-upload-a-file-in-mvc-6-under-vnext and http://stackoverflow.com/questions/29836342/mvc-6-httppostedfilebase especially this answer http://stackoverflow.com/a/27980038/5426333 – adem caglin Apr 16 '16 at 09:05
  • thanks for comment , i actually found that `IFormFile file in Request.Form.Files` now i am stucked at getting the parameter values – Ironsun Apr 16 '16 at 10:11
  • `Request.Form.Files[fileName]` ? – Matheus Miranda Mar 07 '19 at 19:05

3 Answers3

59

This is working code from a recent project. Data has been moved from Request.Files to Request.Form.Files. In case you need to convert stream to byte array - this is the only implementation that worked for me. Others would return empty array.

using System.IO;
var filePath = Path.GetTempFileName();
foreach (var formFile in Request.Form.Files)
{
   if (formFile.Length > 0)
   {
      using (var inputStream = new FileStream(filePath, FileMode.Create))
      {
         // read file to stream
         await formFile.CopyToAsync(inputStream);
         // stream to byte array
         byte[] array = new byte[inputStream.Length];
         inputStream.Seek(0, SeekOrigin.Begin);
         inputStream.Read(array, 0, array.Length);
         // get file name
         string fName = formFile.FileName;
      }
   }
}
Kiaurutis
  • 827
  • 7
  • 10
5

This code works for 100% for both files uploaded using regular form or ajax:

[HttpPost]
public async Task<IActionResult> Upload(IList<IFormFile> files)
{
  foreach (IFormFile source in files)
  {
    string filename = ContentDispositionHeaderValue.Parse(source.ContentDisposition).FileName.Trim('"');

    filename = this.EnsureCorrectFilename(filename);

    using (FileStream output = System.IO.File.Create(this.GetPathAndFilename(filename)))
      await source.CopyToAsync(output);
  }

  return this.RedirectToAction("Index");
}

private string EnsureCorrectFilename(string filename)
{
  if (filename.Contains("\\"))
    filename = filename.Substring(filename.LastIndexOf("\\") + 1);

  return filename;
}

private string GetPathAndFilename(string filename)
{
  return this.HostingEnvironment.WebRootPath + "\\files\\" + filename;
}
Dmitry Sikorsky
  • 1,374
  • 14
  • 23
5

What about this merge from two good solutions I came around :

var myBytes  = await GetByteArrayFromImageAsync(Request.Form.Files[0]); 

private async Task<byte[]> GetByteArrayFromImageAsync(IFormFile file)
{
  using (var target = new MemoryStream())
  {
    await file.CopyToAsync(target);
    return target.ToArray();
  }
}
Carl Verret
  • 576
  • 8
  • 21