开发者

How to launch the download of a file without leaving the current page?

开发者 https://www.devze.com 2023-02-14 05:16 出处:网络
How can I force the download of a file on a ASP.NET page using datas from that page\'s ViewState? I found examples suggesting:

How can I force the download of a file on a ASP.NET page using datas from that page's ViewState? I found examples suggesting:

Response.ClearContent();
Response.ContentType = "text/plain";
Response.AddHeader("Content-Disposition", "attachment; filename=" + "example.aaa");
Response.Write( this.ViewState["stuffs"] );
Response.End();

But if I do that the current page will be erased and return blank to the user.

Other examples says to create "download.aspx" and request it by javascript, but on a this new page I won't have access to 开发者_开发问答the ViewState data of the first page.

The point is, how can I create a file, launch the download and don't lose the current page status?


After more test I found a solution.

If I remove the Response.ClearContent(); from the example code, it will work exactly as I need:

  • creating the new file using the actual page data
  • launching the download of this file
  • preserving the actual page intact


I usually handle this by using Crystal Reports and generating a PDF file. I save a reference to that file with the submitted data and serve it up when requested. You'll want to protect the file so a random URL hacker can't stumble upon it.

0

精彩评论

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