内容处理302重定向 [英] Content-Disposition with 302 redirect

查看:243
本文介绍了内容处理302重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天晚上这个工作正常,但我一定是不小心改了什么,因为现在不是。



我想要做的事情应该从这些标题:

 内容处理:附件;文件名= english_customizable.xml 
地点:http://tortoisewrath.com /files/2.xml

然而,当发送这个头文件时, Content-Disposition 部分在重定向后不起作用。



...为什么?

解决方案

你试图做的是不宜检查这个问题; 标题位置+内容处理



Content-Disposition + Location header

但是您可以这样做,为了使其发挥作用,您必须在发送之前缓冲整个响应。你可以通过输出缓冲来做到这一点。



否则,在下载文件之前,浏览器可能会解释 Location 标题。这是粗略的任何方式,所以你不应该这样做。



请注意使用<$ c $强制'另存为' c> Content-Disposition:attachment; 将确保客户端不会在任何地方进行/导航,因此下面的方法本身应该没问题。

在php中传输文件

仅仅引用一个在正确的地方开了脑筋的家伙

  //要将header()与'content-type'一起使用,为什么不使用mime_content_type()函数而不是根据基础检查类型扩展? 
//示例代码:

<?php
$ file =test.docx;
header(Pragma:public);
header('Content-disposition:attachment; filename ='。$ file);
header(Content-type:.mime_content_type($ file));
header('Content-Encoding:identity');
ob_clean();
flush();
readfile($ file);
?>

//使用$ file映射到任何类型的文件。
//注意:mime类型应该已经在apache设置中定义

来源:< a href =http://www.php.net/manual/en/function.header.php#107581 =nofollow noreferrer> http://www.php.net/manual/en/function.header .php#107581



请注意,原始答案使用 Content-Transfer-Encoding 实际上存在于HTTP中。该来源下面的评论解释说: http://www.php。 net / manual / en / function.header.php#107044

This was working last night, but I must have accidentally changed something, because it isn't now.

What I am trying to do should be clear from these headers:

Content-Disposition: attachment;filename=english_customizable.xml
Location: http://tortoisewrath.com/files/2.xml

However, when this header is sent, the Content-Disposition part doesn't work after the redirect.

...Why?

解决方案

What you're trying to do is inadvisable check this question; Header Location + Content Disposition

Content-Disposition + Location header

But you can do it, to make it work you will have to buffer your whole response before sending it. You can do this with output buffering

Else the browser may interpret the Location header before the file is downloaded. It's sketchy either way, so you shouldn't want to do this.

Please note that forcing 'save as' using Content-Disposition: attachment; will make sure the client doesn't go/navigate anywhere, so the method below on its own should be fine in any case.

Streaming a file in php

To just quote a guy who has his brains in the right place:

// To use header() with 'content-type', why don't you use mime_content_type() function rather than checking the type on the basis of extension? 
// Example code: 

<?php 
$file="test.docx"; 
header("Pragma: public"); 
header('Content-disposition: attachment; filename='.$file); 
header("Content-type: ".mime_content_type($file)); 
header('Content-Encoding: identity'); 
ob_clean(); 
flush(); 
readfile($file); 
?> 

// Use $file to map to whichever type of file. 
// Note: the mime types should already be defined in apache settings

Source: http://www.php.net/manual/en/function.header.php#107581

Note that the original answer used Content-Transfer-Encoding which doesn't actually exist in HTTP. The comment below that source explains it: http://www.php.net/manual/en/function.header.php#107044

这篇关于内容处理302重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆