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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?