简体   繁体   English

上传文件以创建目录ASP.Net Core Mac OS不会创建子文件夹

[英]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.

相关问题 在ASP.NET中创建文件夹并将图像上传到该文件夹​​的最佳方法? - Best way to create a folder and upload a image to that folder in ASP.NET? 文件上传到ASP.NET Core中的wwwroot文件夹 - File upload to wwwroot folder in ASP.NET Core 如何在 asp.net 内核中获取项目的根目录。 Directory.GetCurrentDirectory() 在 Mac 上似乎无法正常工作 - How to get root directory of project in asp.net core. Directory.GetCurrentDirectory() doesn't seem to work correctly on a mac ASP.NET C#-动态创建上传目录? - ASP.NET C# - Create upload directory on the fly? ASP.NET 内核 — 上传.json 文件并转换为列表<t></t> - ASP.NET Core — Upload .json file and convert to List<T> 离线构建 .NET Core 应用程序不会创建“NuGetFallbackFolder”文件夹 - Building of .NET Core application offline doesn't create the "NuGetFallbackFolder" folder ASP.NET文件上传在Windows Azure中不起作用 - ASP.NET file upload doesn't work in windows azure 大文件无法在ASP.NET中上传 - Large file doesn't upload in ASP.NET Asp.net-从头开始创建自定义文件上传控件 - Asp.net - Create a custom File Upload Control from scratch 无法在ASP.NET Core 2.0中创建迁移 - Can't create migration in asp.net core 2.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM