Reputation: 303
I have created asp.net core 2.2 web API + MVC project and I am trying to upload a file to WWWRoot folder. I get access denied error when trying to upload file.
I would like to know appropriate accounts and required permissions to enable write access
I have applied UseStaticFiles in Startup file.
Here's code to upload file:
public async Task<IActionResult> Create(FileuploadRequest fileuploadRequest)
{
if (!ModelState.IsValid)
{
return BadRequest();
}
else
{
if (fileuploadRequest.Logo.Length > 0)
{
string webRootPath = _hostingEnvironment.WebRootPath;
var folderPath = Path.Combine(webRootPath,"Resources\\Images");
using (var fileStream = new FileStream(folderPath, FileMode.Create))
{
await fileuploadRequest.Logo.CopyToAsync(fileStream);
}
Upvotes: 1
Views: 2933
Reputation: 20116
As Joe suggested ,make sure that the folder exists and try to append the fileName to your folder path like below:
if (fileuploadRequest.Logo.Length > 0)
{
string webRootPath = _hostingEnvironment.WebRootPath;
var fileName = Path.GetFileName(fileuploadRequest.Logo.FileName);
var folderPath = Path.Combine(webRootPath,"Resources\\Images",fileName);
using (var fileStream = new FileStream(folderPath, FileMode.Create))
{
await fileuploadRequest.Logo.CopyToAsync(fileStream);
}
Upvotes: 1
Reputation: 20102
Using below of code work for me
public async Task<string> UploadAssets(IFormFile file, string fileName)
{
string storePath = "";
try
{
storePath = Path.Combine(_assetSettings.Value.StorePath, $"{fileName}.{file.ContentType.Split("/")[1]}");
using (var stream = new FileStream(storePath, FileMode.Create))
{
await file.CopyToAsync(stream);
}
}
}
Also try to run your Visual studio in Administrator right to allow full access to file, folder in your computer
Upvotes: 0