63

I'm trying to save a file on disk using this piece of code.

IHostingEnvironment _hostingEnvironment;
public ProfileController(IHostingEnvironment hostingEnvironment)
{
   _hostingEnvironment = hostingEnvironment;
}

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

        var filePath = _hostingEnvironment.WebRootPath + "\\wwwroot\\" + fileName;
        await file.SaveAsAsync(filePath);
    }
    return View();
}

I was able to replace IApplicationEnvironment with IHostingEnvironment, and ApplicationBasePath with WebRootPath.

It seems like IFormFile doesn't have SaveAsAsync() anymore. How do I save file to disk then?

Nkosi
  • 191,971
  • 29
  • 311
  • 378
Richard77
  • 17,505
  • 36
  • 124
  • 222

2 Answers2

102

A few things have changed since core's release candidates

public class ProfileController : Controller {
    private IWebHostEnvironment _hostingEnvironment;

    public ProfileController(IWebHostEnvironment environment) {
        _hostingEnvironment = environment;
    }

    [HttpPost]
    public async Task<IActionResult> Upload(IList<IFormFile> files) {
        string uploads = Path.Combine(_hostingEnvironment.WebRootPath, "uploads");
        foreach (IFormFile file in files) {
            if (file.Length > 0) {
                string filePath = Path.Combine(uploads, file.FileName);
                using (Stream fileStream = new FileStream(filePath, FileMode.Create)) {
                    await file.CopyToAsync(fileStream);
                }
            }
        }
        return View();
    }
}
Nkosi
  • 191,971
  • 29
  • 311
  • 378
  • 8
    you should not use file.FileName from the user input and directly combine it with path.combine, as this file name could contain routing to subdirectories ("../../") you always need to recheck with e.g. Path.GetFullPath(generatedPath) if the return value is the same as your wanted upload directory. Also the filename from the request is not unique. – cyptus Mar 19 '18 at 14:22
  • I just get a `DirectoryNotFoundException`, as `filePath` points to `C:\relative\path` rather than `~\relative\path` .. – Bassie May 04 '18 at 03:48
  • 2
    Dont forget to close the filestream. – Signcodeindie Sep 11 '18 at 10:01
  • 10
    @Signcodeindie the `using` statement will close and dispose of the stream when it goes out of scope. – Nkosi Sep 11 '18 at 10:10
  • 1
    In my case file was not getting save unless I called Dispose, probably a code issue. – Signcodeindie Sep 15 '18 at 15:22
  • 1
    Me too. Without that *fileStream.Close()* it would create the file... but with a filesize of 0. You definitely need to close the stream, not just leave the *using* to dispose of it. – Mike Gledhill Sep 19 '20 at 20:25
  • 1
    It appears we should use `IWebHostEnvironment` instead of `IHostingEnvironment` by now – cwhsu Oct 25 '20 at 14:58
  • 1
    @cwhsu yes. That is the most recent version. This is a very old post when you look at how frequently the framework has be updated. I have updated the post to reflect current changes – Nkosi Oct 25 '20 at 15:01
2

There are to be further changes in Core 3.0 as IHostingEnvironment is now marked as obsolete.

using Microsoft.Extensions.Hosting;

public class ProfileController  : Controller
{
    private IHostEnvironment _hostingEnvironment;

    public ProfileController(IHostEnvironment environment)
    {
        _hostingEnvironment = environment;
    }
Eric Hewett
  • 537
  • 7
  • 16
  • 3
    Don't forget, with the new IHostEnvironment, you need to replace _hostingEnvironment.WebRootPath with _hostingEnvironment.ContentRootPath – Ian Gibblet Oct 15 '20 at 14:16