sreejith
sreejith

Reputation: 23

Azure error Access to the path is denied

After Deployment , I am trying to store a pdf file to directory using

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

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

But I am getting an error

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

I have added Content\UserImages directory in wwwroot afer connecting through ftp. How to solve this?

Upvotes: 1

Views: 2604

Answers (1)

Fei Han
Fei Han

Reputation: 27793

I have added Content\UserImages directory in wwwroot afer connecting through ftp. How to solve this?

You could try to create the directory if it does not exist.

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

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

Besides, if possible, you could store your static files in Azure Blob storage.

Edit: I put source image SourceImg.png in UserImages folder, and I could read the source file into a byte array and write it to the other 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);
}

If I check the UserImages folder, I could find that the 6d8938df-aa4f-40e4-96e3-b2debb6ed992.png is created.

enter image description here

Upvotes: 1

Related Questions