[英]File upload to create directory ASP.Net core mac os doesn't create sub folder
I'm using asp.net core to create a project to upload the file in specific location in the server. 我正在使用asp.net core创建一个项目,以将文件上传到服务器中的特定位置。
Here is the code: 这是代码:
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);
Save the file: 保存文件:
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);
}
}
}
Since the file Get uploaded But it doesn't create sub folder on mac. 由于文件已上传但在Mac上未创建子文件夹。 images\\UploadFile this is not working.
images \\ UploadFile这不起作用。
Can't reproduce this issue, but since directory separator is not \\ on Mac, I guess it does not interpret it correctly. 无法重现此问题,但是由于在Mac上目录分隔符不是\\,我想它不能正确解释它。 You could replace your code with the usage of
Path.DirectorySeparatorChar
constant to avoid this. 您可以使用
Path.DirectorySeparatorChar
常量来替换代码,以避免这种情况。
var directiveToUpload = Path.Combine(_hostingEnvironment.WebRootPath, "images\\UploadFile");
Would become 会成为
var directiveToUpload = Path.Combine(_hostingEnvironment.WebRootPath, $"images{Path.DirectorySeparatorChar}UploadFile");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.