douke1954 2016-03-24 10:50
浏览 48
已采纳

无法将文件发送给浏览器/用户

I'm trying to send a zip file to the user.

$file_info = get_file_info($filepath_name);
header("Content-Type: " . get_mime_by_extension($filepath_name));
header("Content-Length: " . $file_info["size"]);
header_remove('Pragma');
header_remove('Cache-Control');
//header("Content-Type: application/force-download");
header('Content-Type: application/octet-stream');        
header('Content-Disposition: attachment; filename='.urlencode($filepath_name));
header('Location: file://'.$filepath_name);
readfile($filepath_name);

$filepath_name is set to "D:\dev2.local\storage_users\1\export_data\course_2357.zip".

readfile() returns the correct size of file but the file is still not served for downloading.

I've tried all the combinations of headers settings to no avail.

  • 写回答

4条回答 默认 最新

  • duandao2306 2016-03-29 10:30
    关注

    Problem found:

    This functionality was called via AJAX call, thats why php didnt allow file to be send.

    Solution found: Had to return file path and name back to js from where php was ajaxed and serve file from js.

    EDIT: this is the workaround I used:

                                    var hostname = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');
    
                                jQuery('<iframe>', {
                                   src: hostname + value.return_zip,
                                   id:  'iFrame_zip_download',
                                   frameborder: 0,
                                   scrolling: 'no'
                               }).appendTo('#export_import_status');
    

    Where value.return_zip is relative path to file on server (/storage_users/xx/xx/xx.zip). I then add hostname and it turns out nicely for example: https://www.google.com/storage_users/xx/xx/xx.zip. iFrame is hidden in status div.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探