Reputation: 23
I am new to development in ASP.NET Core. I am attempting to set up an app that allows specific users to upload files to my webserver. I have successfully uploaded files to a temp directory but would like to save files in 'wwwroot/images' instead of a temporary directory.
Here is a listing of the controller handling the file uploading:
[HttpPost("FileUpload")]
[DisableFormValueModelBinding]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Index(List<IFormFile> files)
{
long size = files.Sum(f => f.Length);
var filePaths = new List<string>();
foreach (var formFile in files)
{
if (formFile.Length > 0)
{
/*full path to file in temp location*/
var filePath = Path.GetTempFileName();
filePaths.Add(filePath);
using (var stream = new FileStream(filePath, FileMode.Create))
{
await formFile.CopyToAsync(stream);
}
}
}
/*
* Process uploaded files. Don't rely on or trust the FileName
* property without validation.
*/
return Ok(new { count = files.Count, size, filePaths });
}
As you can see, the controller stores the uploaded file to a temp directory. Can anyone show me how to manually specify a location to store file in?
Startup.cs contains the following code:
/*To list physical files from a path provided by configuration:*/
var physicalProvider = new PhysicalFileProvider(Configuration.GetValue<string>("StoredFilesPath"));
services.AddSingleton<IFileProvider>(physicalProvider);
I believe these two lines allow me to specify which directory I would like the uploaded files saved to, as a string in my appsettings.json file:
/wwwroot/images
As stated above, I'm still very new to ASP.NET Core web development. So, if I have neglected to include any pertinent information, please let me know what's missing from my posting and I'll do my best to update my listing. If anyone could provide me with some direction on how to achieve this functionality, I would greatly appreciate it.
Thank you for your help.
Upvotes: 1
Views: 8510
Reputation: 1811
You don't need PhysicalFileProvider
in this case. You can easily inject IWebHostEnvironment
(in previous asp.net core versions was IHostingEnvironment
and now is deprecated).
public string _rootPath;
public HomeController(IHostingEnvironment env)
{
_rootPath = env.WebRootPath;
}
Then in your Index action method just replace this line:
var filePath = Path.GetTempFileName();
with this:
var filePath = Path.Combine(_rootPath, formFile.FileName);
In Path.Combine if you want to store somewhere in wwwroot, and do that dynamically then add one more parameter between _webRootPath and fileName in format files\\images\thumbnails
or files/images/thumbnails
. This will store data in your wwwroot/files/images/thumbnails
.
This example is specific for wwwroot
if you want to store to some another directory outside then change line _rootPath = env.WebRootPath;
to _rootPath = env.ContentRootPath;
Upvotes: 2
Reputation: 3195
IHostingEnvironment
has webRoot
which gives the path for wwwroot. Append the file name with webRoot to get the filePath.
string folder = env.webRoot;
string fileName = ContentDispositionHeaderValue.Parse(formFile.ContentDisposition).FileName.Trim('"');
string fullPath = Path.Combine(folder, fileName);
Note: env
is injected in constructor as parameter.
Upvotes: 1