拒绝访问路径 - 与用户

时间:2015-06-10 13:19:38

标签: c# asp.net pdf download

我在ASP.Net中有一个网页& C#。此页面显示包含我们订单的表格。然后供应商需要检查数据并保存。

我的问题:当供应商点击"保存"时,会下载PDF。我们有超过100家供应商使用本网站,它适用于98%的供应商。但是当3个供应商点击"保存" :

  

访问路径" C:\ ExterneData \ PDF \ F000001.pdf"被拒绝。

这是用于访问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();

我不明白的是这条消息告诉我一些访问权限错误。但它对我和98%的供应商都有效。所以错误不是来自服务器?

1 个答案:

答案 0 :(得分:0)

如果您拥有所有权限,那么我能想到的唯一想法是以不同的方式尝试(这些供应商的PDF是否比其他供应商更大?)。我不认为需要在这里创建2字节数组或使用会话变量。也许是这样的:

// 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();