Reputation: 7122
I'm debugging my NetCore 3.1 project in Visual Studio 2019. I am doing this locally using the built-in IIS Express on Windows 10.
I am trying to save files to my D: drive.
I'm getting the error below when I try to copy a file to the local file system when running my Visual Studio project in debug mode using IIS Express:
{"Access to the path 'D:\\biologyMedia\\eb4cf4c2-6434-4cfe-9fa8-0033bc9b1a08' is denied."}
The error happens in the method below in the try block:
public async Task<bool> CopyFile(IFormFile examFile, Guid diseaseId)
{
string path = @"D:\biologyMedia\" + diseaseId + @"\";
if (!Directory.Exists(path))
{
DirectoryInfo di = Directory.CreateDirectory(path);
}
try
{
using (var fileStream = new FileStream(path, FileMode.Create))
{
await examFile.CopyToAsync(fileStream);
}
} catch (Exception e)
{
var error = e.InnerException;
return false;
}
return true;
}
Based on previous answers, I've tried the following:
I'm not sure what else I can try.
Anyone ever fix this before?
Thanks!
Upvotes: 2
Views: 223
Reputation: 7215
I suggest you save the files to a folder under the root of your application. While I can't explain the problem you are having, you will be much better off writing your code in such a way that it doesn't access folders outside your app root.
Upvotes: 1
Reputation: 176
Skye, sorry, but could it be that your path in
using (var fileStream = new FileStream(path, FileMode.Create))
{
await examFile.CopyToAsync(fileStream);
}
is actually the folder @"D:\biologyMedia\" + diseaseId + @"\"; and therefore not a filename?
So you are trying to copy a file ONTO a folder not INTO it?
Upvotes: 2
Reputation: 176
I have had similar adventures in the past, with IIS, but I have never worked with IIS Express. In my case I had to grant the application pool user access to the folder.
I suggest you check your applicationhost.config to examine your application pools in IIS Express. And ensure that the application pool of IIS Express has complete control of the folder D:\biologyMedia
.
Hope this helps.
Upvotes: 2