Access to the path is denied - different from user

1.3k Views Asked by At

I have a web page in ASP.Net & C#. This page show a table with our orders. Then the suppliers need to check data and save them.

My problem : When suppliers click on "Save", a PDF is downloaded. We have more than 100 suppliers who use this website, it works for 98% of our suppliers. But 3 suppliers have an error message when they click on "save" :

Access to the path "C:\ExterneData\PDF\F000001.pdf" is denied.

This is the code used to access the PDF :

// Save the document...
string filename = Server.MapPath("~/PDF/" + Url_SupplierId + ".pdf");
document.Save(filename);

string path = filename;
string name = Url_SupplierId + ".pdf";

FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);

// Create a byte array of file stream length
byte[] _downFile = new byte[fs.Length];

//Read block of bytes from stream into the byte array
fs.Read(_downFile, 0, System.Convert.ToInt32(fs.Length));

//Close the File Stream
fs.Close();
Session["PDFControl"] = _downFile;
Session["PDFControlName"] = Url_SupplierId + "_" + Url_PurchId + ".pdf";

if (File.Exists(filename))
   File.Delete(filename);

byte[] _downFile2 = Session["PDFControl"] as byte[];
Session["PDFControl"] = null;

Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=" + Session["PDFControlName"] + "; size=" + _downFile2.Length.ToString());
Response.BinaryWrite(_downFile2);
Response.Flush();
Response.End();

The thing I don't understand is this message show me some access right error. But it works for me and 98% of our suppliers. So the error doesn't come from the server ?

1

There are 1 best solutions below

0
On

If you've got all the permissions correct, then the only think I can think of is to try it a different way (do these suppliers have bigger PDFs than the others?). I don't see the need to create 2 byte arrays or use session variables here. Maybe something like this:

// Save the document...
string filename = Server.MapPath("~/PDF/" +  Url_SupplierId + ".pdf");
document.Save(filename);


using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
  {
    // Create a byte array of file stream length
    byte[] _downFile  = new byte[fs.Length];

    int numBytesToRead = (int)fs.Length;
    int numBytesRead = 0;

    //Read block of bytes from stream into the byte array
    while (numBytesToRead > 0)
      {
        // Read may return anything from 0 to numBytesToRead. 
        int n = fs.Read(_downFile, numBytesRead, numBytesToRead);

        // Break when the end of the file is reached. 
        if (n == 0)
          break;

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

if (File.Exists(filename))
 File.Delete(filename);

Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filename) + "; size=" + numBytesToRead);
Response.BinaryWrite(_downFile);
Response.Flush();
Response.End();