drwf69817 2013-08-29 20:28
浏览 63
已采纳

为什么此代码没有将文件下载到我的浏览器?

I have been trying to generate and output a plist file to the user on demand. When the user clicks a button, I run the code below:

<?php
    header('Content-Description: File Transfer');
    header('Content-Type: application/xml');
    header('Content-Disposition: filename="Settings.plist"');

    echo '<?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
          <plist version="1.0">
          <dict>
              <key>key</key>
              <string>value</string>
          </dict>
          </plist>';
 ?>

Here is the output:

enter image description here

What do I need to do to get the file download to initiate?

  • 写回答

3条回答 默认 最新

  • douao8204 2013-08-29 20:31
    关注

    The syntax you're using for the Content Disposition header is wrong. It looks like you forgot to add the Content-Disposition: attachment bit.

    The RFC 6266 shows the syntax through an example:

    Content-Disposition: Attachment; filename=example.html
    

    You're currently doing:

    header('Content-Disposition: filename="Settings.plist"');
                                ^
    

    This should actually be:

    header('Content-Disposition: Attachment; filename="Settings.plist"');
    

    Full code:

    header('Content-Description: File Transfer');
    header('Content-Type: application/xml');
    header('Content-Disposition: Attachment; filename="Settings.plist"');
    

    Refer to RFC 6266 (regarding the use of the Content-Disposition header field in the Hypertext Transfer Protocol (HTTP)) for more details.

    Hope this helps!

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效