我有一個網站,有一堆的預先創建和坐在網絡服務器上的PDF。我可以顯示PDF,但不允許在網站中鏈接它嗎?
我不想讓用戶只需鍵入URL,並獲得PDF文件(即http://MySite/MyPDFFolder/MyPDF.pdf)
我只想讓我的時候加載它們,並顯示他們他們觀看。
我以前做過類似的事情。我用PDFSharp在內存中創建一個PDF,然後將其加載到這樣的頁面:
protected void Page_Load(object sender, EventArgs e)
{
try
{
MemoryStream streamDoc = BarcodeReport.GetPDFReport(ID, false);
// Set the ContentType to pdf, add a header for the length
// and write the contents of the memorystream to the response
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", Convert.ToString(streamDoc.Length));
Response.BinaryWrite(streamDoc.ToArray());
//End the response
Response.End();
streamDoc.Close();
}
catch (NullReferenceException)
{
Communication.Logout();
}
}
我試圖用這個代碼從文件中讀取,但無法弄清楚如何讓一個MemoryStream閱讀在一個文件中。
我還需要一種方式來說「/ MyPDFFolder」路徑是不可瀏覽的。
感謝您的任何建議
感謝您的示例代碼。 – Vaccano 2009-08-03 18:05:20
如果要防止直接訪問(用戶,其他網站鏈接或基於搜索引擎),我會使用上述方法,但將其包裝在處理程序(ashx)中。然後,您可以驗證請求的來源並相應地將其提供出來。 – jaredmroberts 2009-08-03 18:38:25