gpt4 book ai didi

php - 如何在不提示下载的情况下在浏览器中显示以字节形式返回的图像?

转载 作者:可可西里 更新时间:2023-11-01 16:39:35 25 4
gpt4 key购买 nike

我已经编写了以下 PHP 函数,但仍然得到下载文件的提示:

function navigateToBytes($contentType, $bytes){
header('Content-Type: ' .$contentType);
//header('Content-Transfer-Encoding: binary'); // UPDATE: as pointed out this is not needed, though it does not solve the problem
header('Content-Length: '.strlen($bytes));
ob_clean();
flush();
echo $bytes;
}

函数调用示例:

navigateToBytes('image/jpeg', $bytes); // UPDATE: turns out this does work, using image/tiff for tiff images is when the browser does not display the image

其中 $bytes 是从文件中读取的字节数。

最佳答案

抱歉 - 结果我遇到了问题,因为当我使用 JPEG 时,我测试的图像是 TIFF(内容类型正确设置为图像/tiff),浏览器会显示图像!

最终由浏览器决定是否可以显示您发送的 Content-Type。

为了记录,我需要更改的唯一 header 是

Content-Type, 

我应该设置

Content-Length 

也是,除非我设置

Transfer-Encoding: chunked

关于php - 如何在不提示下载的情况下在浏览器中显示以字节形式返回的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4383936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com