![](/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.