假設有這麼一段代碼:
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
在調用CreatePdf()的時候碰到了以下錯誤:
Cannot access a closed Stream.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.ObjectDisposedException: Cannot access a closed Stream.
Source Error:
Line 58: private void ViewPdf(Stream fs)
Line 59: {
Line 60: byte[] buffer=new byte[fs.Length]; Line 61: fs.Position=0;
Line 62: fs.Read(buffer,0,(int)fs.Length); |
問題出在哪裏了呢?從錯誤我能夠知道咱們準備操做的Stream已經關閉,這是由於iTextSharp自動關閉生成的Stream了,那有沒有辦法不關閉呢?
看了下面這段代碼,也許就不用我說什麼了:
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
原來PdfWriter有個熟悉就是讓咱們設置是否自動關閉Stream的,而默認是關閉的。
2006-12-01更新ViewPdf function
private
void
ViewPdf(Stream fs)
{
byte [] buffer = new byte [fs.Length];
fs.Position = 0 ;
fs.Read(buffer, 0 , ( int )fs.Length);
Response.Clear();
// Response.AddHeader("Content-Disposition", "attachment;FileName=out.pdf");
Response.AddHeader( " Content-Length " ,fs.Length.ToString());
Response.ContentType = " application/pdf " ;
fs.Close();
Response.BinaryWrite(buffer);
Response.OutputStream.Flush();
Response.OutputStream.Close();
}
若是須要下載而不是在IE中看的話,請把Response.AddHeader("Content-Disposition", "attachment;FileName=out.pdf");前的註釋去掉,並替換上你想要的名字。
{
byte [] buffer = new byte [fs.Length];
fs.Position = 0 ;
fs.Read(buffer, 0 , ( int )fs.Length);
Response.Clear();
// Response.AddHeader("Content-Disposition", "attachment;FileName=out.pdf");
Response.AddHeader( " Content-Length " ,fs.Length.ToString());
Response.ContentType = " application/pdf " ;
fs.Close();
Response.BinaryWrite(buffer);
Response.OutputStream.Flush();
Response.OutputStream.Close();
}
若是不指定 Response.AddHeader( " Content-Length " ,fs.Length.ToString());,IE會把網頁內容也輸出,在PDF後面,此時顯示生成的PDF被破壞(這個說是IE的一個bug,我也不清楚)
若是附件名爲中文:
Response.AddHeader("Content-Disposition", "attachment;FileName="+HttpUtility.UrlEncode("中文.pdf"));
內嵌顯示PDF
Response.AddHeader("Content-Disposition", "inline;FileName=out.pdf");
更多這方面的信息google上搜索吧。
另一個和緩存有關的信息,Cache-Control,須要的本身研究下吧。