开发者

Response closes after export document

开发者 https://www.devze.com 2023-03-03 22:42 出处:网络
I have been trying to export a Word document into Response using ASP.Net. So I achieved this goal with this code.

I have been trying to export a Word document into Response using ASP.Net. So I achieved this goal with this code.

Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.ContentEncoding = Encoding.UTF8;
Response.Charset = "UTF-8";
Response.Buffer = true;

this.EnableViewState = false;
Response.ContentType = "application/msword";
Response.AddHeader("content-disposition", "attachment;filename=ExportedData" + Guid.NewGuid().ToString().Remove(5) + ".docx");

var wordData = this.RequestService.ExportToWord(this.RequestItem);

Response.BinaryWrite(wordData);
Response.Flush();

//Response.Clear();
//Response.End();
//Response.Close();
//RedirectToSamePage();
//Response.Flush();

But the problem is, when user export tha page as word document, the other functionality of the page disappears. Like, you can't click to another ASP control, because endi开发者_StackOverflowng Response object, kills ASP controls connection with DLL. So, as you can see from the commented codes, I have been calling some Response object functions, but still I can't manage other ASP controls to work after Export operation. So, how can I manage that?

Thanks in advance.


You are getting this behaviour because you are altering the response object for current page. Because the page postback, the response object is used for rendering the latest page content which is now the Word binary content.

I will suggest that you do one of the below:

  • Bring up a new page and implement the above code on the new page to push the Word document
  • Have an iframe (you may not want to display it) and set the source to the page that will push the Word content. This will allow you to stay on current page without interfering with the flow.


I would make a second page and put all the word export stuff in the second page. Then in your original aspx page either do a response.redirect to this new page or a window.location to it and the new page will simply stream back the document. This way the user stays on the original page and everything operates normally.

I recommend using the session before redirect to the new page to pick up any parameters you need in order to generate the word document. However, the query string will work as well.

0

精彩评论

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