2013-11-22 4 views
0

Я пытаюсь отобразить PDF-файл из данных, сохраненных в моей базе данных. Он находится в формате byte[]. Следующий код успешно отображает PDF-файл на новой вкладке IE, однако я также получаю ошибку исключения в моей инструкции try catch.Отображение PDF с байта [] в C#

Мой пример кода:

try { 

    byte[] byteOutput = myObject.ContractBytes; 
    Response.ClearHeaders(); 
    HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=file.pdf"); 
    HttpContext.Current.Response.ContentType = "application/pdf"; 
    Response.BinaryWrite(byteOutput); 
    Response.Flush(); 
    Response.End(); 
} 
catch (Exception ex) 
{ 
    RecordError(ex.Message, ex); 
} 

Моя ошибка исключение:

Thread was being aborted. 
at System.Threading.Thread.AbortInternal() 
at System.Threading.Thread.Abort(Object stateInfo) 
at System.Web.HttpResponse.AbortCurrentThread() 
at System.Web.HttpResponse.End() 
at GetForm.FromDatabase() in c:\\SRC\\GetPDF.aspx.cs:line 340 
at GetForm.Page_Load(Object sender, EventArgs e) in c:\\SRC\\MyPage.aspx.cs:line 106 

Любая помощь будет оценен по достоинству!

Благодарим вас за продвижение.

+1

Попробуйте вызвать 'this.Context.ApplicationInstance.CompleteRequest()' вместо 'Response.End()' - это закончится ответ и не бросайте 'ThreadAbortException'. –

ответ

1

У вас read the documentation?

This method is provided only for compatibility with ASP—that is, for compatibility with COM-based Web-programming technology that preceded ASP.NET. If you want to jump ahead to the EndRequest event and send a response to the client, it is usually preferable to call CompleteRequest instead.

To mimic the behavior of the End method in ASP, this method tries to raise a [ThreadAbortException] exception. If this attempt is successful, the calling thread will be aborted, which is detrimental to your site's performance. In that case, no code after the call to the End method is executed.

If the End method is not able to raise a [ThreadAbortException], it instead flushes the response bytes to the client. It does this synchronously, which can also be detrimental to your site's performance.

In either case (whether or not a [ThreadAbortException] exception is successfully raised), the response pipeline jumps ahead to the EndRequest event.

The CompleteRequest method does not raise an exception, and code after the call to the CompleteRequest method might be executed. If your intention is to avoid execution of subsequent code, and if the performance penalty of End is acceptable, you can call End instead of CompleteRequest .

+0

Этот код был написан до того, как я занял позицию. Спасибо за ваш ответ, который вы предоставили. – Turp

3

Я думаю, что следующая инструкция генерирует эту ошибку.

Response.End(); 

Зачем вам это нужно? Вы пробовали код без этого вызова?

Смежные вопросы