开发者

Is Response.End() mandatory in the given scenario?

开发者 https://www.devze.com 2023-02-08 02:01 出处:网络
string filePath = ExportAndSaveInvoiceAsHtml(invoiceId); Response.AddHeader(\"Content-Disposition\", \"attachment;filename=\" + Path.GetFileName(filePath));
string filePath = ExportAndSaveInvoiceAsHtml(invoiceId);      

Response.AddHeader("Content-Disposition", "attachment;filename=" + Path.GetFileName(filePath));

Response.WriteFile(filePath);

// Using Response.End() solves the problem
// Response.开发者_StackOverflow社区End();

I want to enable users to download HTML invoice (from gridView of invoices). The resulting file contains the invoice file data + unwanted the page html also.

If i use Response.End(), it solves the problem, but then it always throws exception. Read in some threads that exception is by design. But then is it sure, it wont cause problem like response truncation or so anytime ?

How else can i write JUST the file data.

Btw, earlier we wrote invoice as PDF, and that time not using Response.End() caused incomplete file download.

Can someone give basics of writing custom data/file to Response stream.


That's because the response is buffered. You have to call Response.End() to flush the buffers and ensure the client gets the complete response before the handler thread completes.

The ThreadAbortException thrown by Response.End() is by design and can safely be ignored.


Instead of Response.End() you can use

HttpContext.Current.ApplicationInstance.CompleteRequest()

This method tend to avoid throwing exceptions :)

0

精彩评论

暂无评论...
验证码 换一张
取 消