开发者

Html being added to Download file in response

I'm creating a csv file on the fly in my ASP.NET web 开发者_StackOverflow中文版app and sending it back to the user using the following code

ExportPlacementListPostModel postModel = CreatePostModelFromRequest();
MemoryStream stream = PlacementDatabaseController.ExportPlacementList(postModel);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("content-disposition", "attachment; filename=studentplacement.csv");
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(stream.GetBuffer());

Every time I download the file the web pages html is being appended onto the document.

Can anyone see what i am doing wrong here.

Colin G


Call Response.End() after Response.BinaryWrite to prevent further output being written to the response.


HttpContext.Current.ApplicationInstance.CompleteRequest can be used as well.

The CompleteRequest method causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event.

The Response.End method ends the page execution and shifts the execution to the Application EndRequest event in the application's event pipeline. The line of code that follows Response.End is not executed.

Here's a good read: Don’t use Response.End() with OutputCache

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜