拒绝对路径的访问-与用户不同

本文关键字:用户 访问 路径 拒绝 | 更新日期: 2023-09-27 17:55:04

我有一个网页在ASP。净,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%的供应商都有效。所以错误不是来自服务器?

拒绝对路径的访问-与用户不同

如果所有权限都正确,那么我唯一能想到的就是尝试不同的方法(这些供应商的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();