Search code examples
asp.net-mvcfile-uploadtemporary-files

Where does ASP.NET 5 / MVC6 store uploaded files?


If I upload a file and it's represented by a Microsoft.AspNet.Http.Features.Internal.FormFile, will it have been stored anywhere on the filesystem of the process, or does it point to bytes that simply exist in memory?


Solution

  • FormFile object based on HttpRequest.Body memory stream. See more details on github

    var file = new FormFile(_request.Body, section.BaseStreamOffset.Value, section.Body.Length, name, fileName)
    {
        Headers = new HeaderDictionary(section.Headers),
    };
    

    You can call FormFile.SaveAs() or FormFile.SaveAsAsync() to save file in filesystem:

    public class HomeController : Controller
    {
        public IHostingEnvironment Hosting { get; set; }
    
        public async Task<IActionResult> Index(IFormFile file)
        {
            await file.SaveAsAsync(Path.Combine(Hosting.WebRootPath, "storage", "file.txt"));
            //...
        }
    }