强制下载文件

发布于 2024-12-29 08:31:56 字数 628 浏览 0 评论 0原文

我编写了一个脚本,将数据添加到现有的 csv 文件中。 对于其他用户来说,它也可以正常工作,但对于一个用户来说,文件不可下载,只能在浏览器中打开。

写入文件时使用的标头:

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"file.csv\";" );
header("Content-Transfer-Encoding: binary");

我通过 ajax 发送一些用户选择,并在准备好后导航到文件。

 $.get( urlJson ,function(){

     document.location.href = urlCsv;

  });

我认为这是服务器问题,也许有些东西没有启用,但我不知道到底是什么。

I've wrote a script which add data to already existing csv file.
It is working ok for other users too, but for one user file is not downloadable only opens in browsers.

Header used when writing to file:

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"file.csv\";" );
header("Content-Transfer-Encoding: binary");

I'm sending some user choise by ajax and when it is ready navigate to the file.

 $.get( urlJson ,function(){

     document.location.href = urlCsv;

  });

I think this is server problem maybe something is not enable, but I don't know what exactly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回首观望 2025-01-05 08:31:56

这应该可以做到

<?php

$file = "data.csv";

header("Pragma: public"); 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Content-Type: application/force-download"); 
header("Content-Disposition: attachment; filename=".$file);
header("Content-Description: File Transfer"); 
@readfile($file); 

?>

This should do it

<?php

$file = "data.csv";

header("Pragma: public"); 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Content-Type: application/force-download"); 
header("Content-Disposition: attachment; filename=".$file);
header("Content-Description: File Transfer"); 
@readfile($file); 

?>
猥琐帝 2025-01-05 08:31:56

使用 PHP 强制下载最简单的情况是

 header("Content-Description: File Transfer");
 header("Content-Disposition: attachment; filename=THEFILENAME");
 header("Content-Type: application/THEFILETYPE");
 header("Content-Transfer-Encoding: binary");

,用户浏览器可能只是忽略下载文件的请求(因为这取决于浏览器要做什么),或者可能不知道 application/octet-stream 是什么。

您还有更多信息吗?

CSV 可以是“文本/逗号分隔值、文本/csv、应用程序/csv”,因为它是 MIME 您尝试过这些吗?

Forcing a download with PHP at its simplest is

 header("Content-Description: File Transfer");
 header("Content-Disposition: attachment; filename=THEFILENAME");
 header("Content-Type: application/THEFILETYPE");
 header("Content-Transfer-Encoding: binary");

It is possible the users browser is just ignoring the request to download the file (as it's up to the browser what to do) or perhaps it doesn't know what application/octet-stream is.

Do you have any more information?

A CSV can be "text/comma-separated-values, text/csv, application/csv" as it's MIME have you tried these?

回首观望 2025-01-05 08:31:56

类似于@Walkerneo所说的......
但是,某些版本的 Internet Explorer 喜欢选择对 application/octet-stream 执行的操作。例如,在我的网站之一上,IE 总是会在媒体播放器中打开 mp3 文件。解决方案是在 .htaccess 文件中添加 AddType mycompany/podcast mp3 。任何像这样的组合类型都会强制 IE 显式下载。

Similar to what @Walkerneo said...
However, some versions of Internet Explorer like to choose what they do with an application/octet-stream. For instance on one of my websites IE would always open mp3 files in media player. The solution was to add AddType mycompany/podcast mp3 in the .htaccess file. Any made up type like this will force IE to explicitly download.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文