Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "application/pdf";
Response.WriteFile(Server.MapPath("temp")+"\\temp.pdf");
// HttpContext.Current.Response.ContentType = "application/Excel";
// HttpContext.Current.Response.WriteFile(Server.MapPath("temp")+"\\temp.xls");
Response.Flush();
Response.End();
------------------------------------------------------
用以上代码在Page_Load 中进行处理,却无法显示出PDF,
但如果在页面中加一按钮,再调用此方法,却可显示,是什么原因呢?
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "application/pdf";
Response.WriteFile(Server.MapPath("temp")+"\\temp.pdf");
// HttpContext.Current.Response.ContentType = "application/Excel";
// HttpContext.Current.Response.WriteFile(Server.MapPath("temp")+"\\temp.xls");
Response.Flush();
Response.End();
------------------------------------------------------
用以上代码在Page_Load 中进行处理,却无法显示出PDF,
但如果在页面中加一按钮,再调用此方法,却可显示,是什么原因呢?
如果用方法2,就可以,但出它会首先弹出窗口提示.
而且也不是直接显示在 IE 中
System.IO.FileInfo fi = new System.IO.FileInfo(Server.MapPath("temp")+"\\temp.pdf");
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Server.MapPath("temp")+"\\temp.pdf", System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length",fi.Length.ToString());
//Response.ContentType="application/octet-stream";
Response.ContentType = "application/pdf";
Response.WriteFile(Server.MapPath("temp")+"\\temp.pdf");
Response.Flush();
Response.End();
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) 最新版本:20070212http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
MSDN 中的例子也是不行.
建立 ASPX 網頁
1. 將名為 BinaryData.aspx 的新 .aspx 網頁新增至目前的專案中,如下所示: a. 在 [方案總管] 中,用滑鼠右鍵按一下專案節點,按一下 [加入],然後按一下 [加入 Web Form]。
b. 將網頁命名為 BinaryData.aspx,然後按一下 [開啟]。注意:確定網頁已加入到專案中且與前一節加入的 .pdf 檔案位在相同層級。此點非常重要,因為程式碼是使用相對路徑來對 .pdf 檔案進行初始參照。
2. 在編輯器中,用滑鼠右鍵按一下 BinaryData.aspx,再按一下 [檢視程式碼]。
3. 反白選取下列程式碼,並用滑鼠右鍵按一下程式碼,再按一下 [複製]。在程式碼後置網頁的 Page_Load 事件中,按一下 [編輯] 功能表上的 [貼上],貼上下列程式碼:private void Page_Load(object sender, System.EventArgs e)
{
//Set the appropriate ContentType.
Response.ContentType = "Application/pdf";
//Get the physical path to the file.
string FilePath = MapPath("acrobat.pdf");
//Write the file directly to the HTTP content output stream.
Response.WriteFile(FilePath);
Response.End();
}
4. 在 [檔案] 功能表上,按一下 [全部儲存]。
5. 在 [建置] 功能表上,按一下 [建置]。
6. 如果要執行程式碼,請用滑鼠右鍵按一下 [方案總管] 中的 BinaryData.aspx,再按一下 [在瀏覽器中檢視]。如果收到提示,請按一下 [開啟],在瀏覽器中開啟並轉譯該檔案。
注意:如果要用先前的程式碼來支援其他二進位檔案類型,您必須修改 ContentType 字串中的值,使其指定適當的檔案格式。此字串的語法通常是 "type/subtype",其中 "type" 為一般的內容類別,而 "subtype" 則是特定的內容類型。如需支援內容類型的完整清單,請參閱 Web 瀏覽器說明文件或目前的 HTTP 規格。下面清單提列了一些常見的 ContentType 值: • "text/HTML"
• "image/GIF"
• "image/JPEG"
• "text/plain"
• "Application/msword" (適用於 Microsoft Word 檔案)
• "Application/x-msexcel" (適用於 Microsoft Excel 檔案)
--------------------
When you say it doesn't work, what do you mean? I've had a problem in the past where displaying a PDF report just results in a blank page in the browser. The way to fix that is deleting your temp internet files.See item 7 on this page: http://www.adobe.com/support/techdocs/328233.htmlI think there's also a MS KB article about this problem, but I can't find it now.
{
string TmpPDFFileName = "E://EboHrData//pdf//111.pdf";
page.Response.ClearContent();
page.Response.ClearHeaders();
page.Response.ContentType = "application/pdf";
page.Response.WriteFile(TmpPDFFileName);
page.Response.Flush();
page.Response.Close();
System.IO.File.Delete(TmpPDFFileName);
return true;
}