![](/img/trans.png)
[英]System.UnauthorizedAccessException: Access to the path:PATH is denied
[英]System.UnauthorizedAccessException: Access to the path '...' is denied
所以,我對 .NET 核心有點陌生,我在將 IFormFile 復制到我的項目中的文件夾時遇到了問題。 我一直在嘗試查看有關它的其他問題,但沒有一個答案對我有幫助。 那是我的代碼:
public async Task<string> AddImage(IFormFile image, int id)
{
if (image.Length > 0)
{
var filePath = Path.Combine("images", ("apartamento" + id.ToString()), image.FileName);
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
await image.CopyToAsync(fileStream);
}
return filePath;
}
}
問題是我似乎沒有權限(努力查看是否所有內容都已獲得授權)或關於我的代碼的某些內容不正確。
我只想做一件簡單的事情:在我的 API 中接收一個圖像並保存它,以便我以后可以在我的項目中使用。 我以前做過,但我在 .NET 核心方面遇到了一些問題。
如果有人有其他想法去做,我會很感激。 我正在考慮在我的數據庫中將圖像保存為 Base64,但我不確定哪個是最佳選擇。 或者最簡單的。
嘗試這個
public async Task<string> AddImage(IFormFile image, int id)
{
if (image.Length > 0)
{
var dir = Path.Combine("images", ("apartamento" + id.ToString()));
var filePath = Path.Combine(dir, image.FileName);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
await image.CopyToAsync(fileStream);
}
return filePath;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.