开发者

HTTP New location after download starts

I'm using X-Accel-Redirect (so implicitly nginx), Content-Type and Content-Disposition to download a file, everything works great.

What I need to accomplish is redirecting to a new location after the download starts.

I've tested with both Refresh and Location, it doesn't 开发者_运维知识库work. Is it possible with HTTP 1.1/nginx?

Addendum

I am looking for a HTTP-only approach. No javascript, no <meta>.

The download starts after processing a POST form.


It's not possible as soon as Nginx is already sending the response. So the only way seems to return 302 Redirect from Nginx location you are referring in X-Accel-Redirect

How would you tell Nginx to do this?

The best way is to pass a custom header to Nginx (along with XAR) from the application

How to trigger processing in Nginx?

You can easily get the header's value using variable $http_VARIABLE, so typical piece of Nginx configuration looks like

if ($http_your_header) {
  rewrite ^ http://your_redirect redirect;
}

See Nginx rewrite module documentation and Nginx HTTP header variable for more information.


<?php

header('refresh:5;url=http://www.google.com/');
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="file.pdf"');
readfile('file.pdf');

That will redirect you to google.com in 5 seconds after the d/l starts ;)


I would change the original download link to open in a new window (target="_blank") and use javascript to redirect. There are several variations you could try based on that.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜