简体   繁体   中英

Url redirect after click and downloading a file

My goal is to redirect or refresh my page while downloding excel file :

Details :

In my application i am sending dynamically generated excel file

setContentType("application/vnd.ms-excel; charset=windows-1254");
setCharacterEncoding(Constants.ENCODING);
setHeader("content-disposition", "attachment;filename=" + Constants.DEFAULT_EXCEL_FILE);
setHeader("Location","http://www.google.com");

But after asking user to save or open excel file, it didn't redirect to ex : google.com.

Is it possible to do it in header part ? If yes please say how .

Thanks.

您无法重定向,因为您已经清空了响应流。

This is simply isn't possible for the reason that the defined behavior of the browser is bound to be vague. In other words, when the browser notes the presence of two headers, each with possibly conflicting requirements, then the behavior might be to simply ignore one, instead of obeying both - it is upto the browser vendor to define this.

In this particular case, the Content-Disposition and the Location HTTP headers are conflicting, more so because if the browser were to process the Location header first resulting in the redirection, the end-user would never be prompted for the file download.

On an additional note, the Location header would only make sense for a HTTP 302 response (I believe that this is not the case in your application, not that it might help).

您可以使用一些JavaScript来完成这项工作

<a href="myfile.txt" onclick='window.location = 'http://www.google.com/';">
File
</a>

This is one option, not in header part tough

private void DownloadFile()

{

//Download file here...

//Refresh this page.
this.Response.Redirect(Request.Url.AbsolutePath);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM