2011-12-30 22 views
2

ASP.Net MVC 3FileStreamResultエラー処理

私は、PDFドキュメントをインポートしてウォーターマークを付けた後にFileStreamResultを返すアクションを持っています。ファイルが見つからないというエラーが発生する可能性があるため、ファイルストリームの代わりにビューを返すにはどうすればよいですか?

フィリップハッチソンのjQuery PDFObject(http://pdfobject.com)を使用して、アクションを呼び出してDIVでレンダリングするので、サーバー側にリダイレクトできません。

これを繰り返す:PDFファイルストリームの結果をDIVに埋め込むページ上のjQueryリンクです。私が考えることができる唯一の「ハック」の事はError.pdfファイルを送ることです。

あなたの考えは?

答えて

0

エラーのPDFメモリストリームが作成されました。

MemoryStream ms = new MemoryStream(); 
try 
    { 
     PdfReader reader = new PdfReader(readerURL); 
     StampWatermark(WO, ms, reader); 
    } 

catch (Exception ex) 
    { 
     RenderErrorPDF(WO, ms, ex); 
    } 

byte[] byteinfo = ms.ToArray(); 
ms.Write(byteinfo, 0, byteinfo.Length); 
ms.Position = 0; 
ms.Seek(0, SeekOrigin.Begin); 
return new FileStreamResult(ms, "application/pdf"); 

RenderErrorPDF方法

private static void RenderErrorPDF(WorkOrder WO, MemoryStream ms, Exception ex) 
    { 
     BaseFont bfTimes = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1250, false); 
     var doc = new Document(new Rectangle(792f, 540f)); 
     var wr = PdfWriter.GetInstance(doc, ms); 
     doc.Open(); 
     doc.Add(new Paragraph("There was an error rendering the file that you requested...", new Font(bfTimes, 24f))); 
     doc.Add(new Paragraph(string.Format("\r\rFile: {0}", WO.DRAWING_FILE))); 

     doc.Add(new Paragraph(string.Format("\r\rError: {0}", ex.Message))); 
     wr.CloseStream = false; 
     doc.Close(); 
    }