Azure错误拒绝访问路径

时间:2017-05-15 08:30:30

标签: azure deployment azure-web-sites

部署后,我正在尝试使用

将pdf文件存储到目录中
string biodataPath = Server.MapPath("~/Content/UserImages/");

string fullBiodataPath = Path.Combine(biodataPath, guid.ToString() + extension);

但我收到错误

Could not find a part of the path 'D:\home\site\wwwroot\Content\UserImages\6d8938df-aa4f-40e4-96e3-b2debb6ed992.png'

我已经在Content\UserImages中添加了wwwroot目录,通过ftp连接。怎么解决这个问题?

1 个答案:

答案 0 :(得分:1)

  

我在通过ftp连接的wwwroot中添加了Content \ UserImages目录。怎么解决这个问题?

如果目录不存在,您可以尝试创建该目录。

string biodataPath = Server.MapPath("~/Content/UserImages/");

if (!Directory.Exists(biodataPath))
{
    DirectoryInfo di = Directory.CreateDirectory(biodataPath);
}

此外,如果可能,您可以将静态文件存储在Azure Blob storage

修改 我将源图像SourceImg.png放在 UserImages 文件夹中,我可以将源文件读入字节数组并将其写入另一个FileStream。

string biodataPath = Server.MapPath("~/Content/UserImages/");
string pathSource = biodataPath + "SourceImg.png";

//the following code will create new file named 6d8938df-aa4f-40e4-96e3-b2debb6ed992.png
string pathNew = Server.MapPath("~/Content/UserImages/") + "6d8938df-aa4f-40e4-96e3-b2debb6ed992.png";

try
{

    using (FileStream fsSource = new FileStream(pathSource,
        FileMode.Open, FileAccess.Read))
    {
        byte[] bytes = new byte[fsSource.Length];
        int numBytesToRead = (int)fsSource.Length;
        int numBytesRead = 0;
        while (numBytesToRead > 0)
        {
            int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);

            if (n == 0)
                break;

            numBytesRead += n;
            numBytesToRead -= n;
        }
        numBytesToRead = bytes.Length;

        // Write the byte array to the new FileStream.
        using (FileStream fsNew = new FileStream(pathNew,
            FileMode.Create, FileAccess.Write))
        {
            fsNew.Write(bytes, 0, numBytesToRead);
        }
    }
}
catch (FileNotFoundException ioEx)
{
    Console.WriteLine(ioEx.Message);
}

如果我查看 UserImages 文件夹,我会发现6d8938df-aa4f-40e4-96e3-b2debb6ed992.png已创建。

enter image description here