我正在使用asp.net核心创建一个项目,以将文件上传到服务器中的特定位置。
代码如下:
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 无效。
答案 0 :(得分:2)
Can't reproduce this issue, but since directory separator is not \ on Mac, I guess it does not interpret it correctly. You could replace your code with the usage of Path.DirectorySeparatorChar
constant to avoid this.
var directiveToUpload = Path.Combine(_hostingEnvironment.WebRootPath, "images\\UploadFile");
Would become
var directiveToUpload = Path.Combine(_hostingEnvironment.WebRootPath, $"images{Path.DirectorySeparatorChar}UploadFile");