【问题标题】:download tar file via php after form submit表单提交后通过php下载tar文件
【发布时间】:2020-11-04 12:20:01
【问题描述】:

我有一个用户填写的表格,然后按“保存”。表单数据通过 AJAX 发送到一个 php 文件进行处理。 php 文件将表单数据写入一个文件并压缩多个文件。然后我希望通过浏览器下载 tar 文件。

当用户按下“保存”时,表单数据被写入文件。表单挂起约 30 秒,tar 文件被写入我的屏幕,表单被清除。下载代码似乎很简单:

  $filename = '/var/www/html/temp.tar';
  ob_clean();
  header( 'Content-Type: archive/tar' );
  header( 'Content-Disposition: attachment; filename="' . basename( $filename ) . '"'  );
  header( 'Content-Transfer-Encoding: binary' );
  header( 'Content-Length: ' . filesize( $filename ) );
  readfile( $filename );

我尝试过各种 Content-type 行,例如

  header( 'Content-Type: application/x-tar' );
  header( 'Content-Type: application/tar' );
  header( 'Content-Type: application/octet-stream' );

我的 httpd/error_log 或 Firefox 控制台中没有错误。 tar 文件存在,为了测试,是 777。

我不知道该怎么办。

【问题讨论】:

    标签: php ajax download


    【解决方案1】:

    尝试使用 'application/gzip' - tar 是压缩文件,它不是 official media type

    【讨论】:

    • 我这样做了,当表单提交时(没有挂起),文件仍然显示在我的屏幕上,而不是自动下载。
    • 更新:表单实际上并没有提交。
    猜你喜欢
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多