1

我无法使用下面的代码行。在我的开发机器(Windows)上它正在工作。是的,我正在 Windows 上开发应用程序并在 Ubuntu 上进行部署。下一个应用程序我不会这样做。

我收到错误消息“访问路径 /var/... 已定义”。

try
{
   Directory.CreateDirectory(dirInfo.FullName + "/" + numDirs);
}
catch(Exception e)
{
   return e.Message; // access to the path /var/... is defined
}

我使用 nginx 作为 Kestrel 的代理服务器。如Microsoft 指南中所述

我尝试随机触发一些权限命令,因为我远非 Ubuntu 专家,但 CreateDirectory 方法仍在产生错误。

我尝试过的权限命令:

sudo chown -R www-data:www-data /var/www/PROJECTDIR

sudo find /var/PROJECTDIR -type d -exec chmod 770 {} \;
sudo find /var/PROJECTDIR -type f -exec chmod 660 {} \;

我没有在 /var/www 中设置我的项目我正在使用 /var/anotherdir/anotherdir 之类的东西,这是一个问题吗?

4

1 回答 1

1

实际上引发异常的是我的 IFormFile 扩展方法。我尝试发布尽可能少的代码以使问题更紧凑,但我认为这不再是一个好主意,我应该在我的项目中发布 try catch 块。在我的代码中,我有这样的东西。obs。我只是在测试,最后我摆脱了文字和过多的变量引用。

    try
{
   Directory.CreateDirectory(dirInfo.FullName + "/" + numDirs);
   file.SaveAs(dirInfo.FullName + "/" + numDirs) // it was this that threw the exception.
}
catch(Exception e)
{
   return e.Message; // access to the path /var/... is defined
}

那是我遇到过的最烦人的错误之一。幸运的是,我在这篇文章中遇到了一个确切的问题 ,解决了我的问题。我只是传递目录。

不知何故,它在 Windows 上可以工作,但在 Linux 上却不行。

 public static void SaveAs(this IFormFile formFile, string filePath)
{
    using (var stream = new FileStream( filePath, FileMode.Create))
    {
        formFile.CopyTo(stream);
    }

}

解决方案:

 public static void SaveAs(this IFormFile formFile, string filePath)
    {
        using (var stream = new FileStream( Path.Combine( filePath, formFile.FileName), FileMode.Create))
        {
            formFile.CopyTo(stream);
        }
    }
于 2018-03-12T11:26:07.157 回答