![](/img/trans.png)
[英]Best way to create a folder and upload a image to that folder in ASP.NET?
[英]File upload to create directory ASP.Net core mac os doesn't create sub folder
我正在使用asp.net core創建一個項目,以將文件上傳到服務器中的特定位置。
這是代碼:
TempFileName = Regex.Replace(vehicle.FileUpload.FileName, "[^a-zA-Z0-9_]+", "_", RegexOptions.Compiled);
var directiveToUpload = Path.Combine(_hostingEnvironment.WebRootPath, "images\\UploadFile");
if (!System.IO.Directory.Exists(directiveToUpload))
{
System.IO.Directory.CreateDirectory(directiveToUpload);
}
await SaveFileToServer(TempFileName);
保存文件:
async Task SaveFileToServer(string FileName)
{
if (vehicle.FileUpload.Length > 0)
{
using (var stream = new FileStream(Path.Combine(directiveToUpload, FileName), FileMode.Create))
{
await vehicle.FileUpload.CopyToAsync(stream);
}
}
}
由於文件已上傳但在Mac上未創建子文件夾。 images \\ UploadFile這不起作用。
無法重現此問題,但是由於在Mac上目錄分隔符不是\\,我想它不能正確解釋它。 您可以使用Path.DirectorySeparatorChar
常量來替換代碼,以避免這種情況。
var directiveToUpload = Path.Combine(_hostingEnvironment.WebRootPath, "images\\UploadFile");
會成為
var directiveToUpload = Path.Combine(_hostingEnvironment.WebRootPath, $"images{Path.DirectorySeparatorChar}UploadFile");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.