开发者

Page rendering and http handlers

开发者 https://www.devze.com 2023-02-25 00:52 出处:网络
We have a page that opens in a new browser window where there\'s an automatic redirect to an ASHX handler that produces some kind of an XLS file. It\'s done using a javascript redirect, i.e. setting w

We have a page that opens in a new browser window where there's an automatic redirect to an ASHX handler that produces some kind of an XLS file. It's done using a javascript redirect, i.e. setting window.location.href to the URL of the ASHX.

Although it works and presents the download dialog for the file, setting window.location.href also clears the content of the window so that it stays blank. It somehow makes sense but still it would be nice to keep the content of the previous page there while opening the download dialog in the foreground. Is it possible somehow (by defering the execution of the redirect or usi开发者_开发技巧ng a different technique to call the ASHX handler) ?

Another nice to have thing would be if we could close the parent page after the download dialog is presented, could this work in any way ?


Dynamically creating (in javascript) an hidden iframe that points to the download ashx location and adding it to the DOM would do the trick.


By using the content-disposition header in your response from your handler you can display the save dialog without having to open a new window. You won't need to use javascript to open a new window or create an iframe.

 response.Clear();
 response.ContentType = "application/vnd.ms-excel";
 response.AddHeader("content-disposition", "attachment; filename=sample.xls");
 response.AddHeader("content-legth", content.Length.ToString());
 response.Write(content.ToString());
 response.End();

See this question for the possible excel mime types ( contentType ) Setting mime type for excel document

0

精彩评论

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