简体   繁体   中英

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.

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. images\\UploadFile this is not working.

图片上传

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM