Heim  >  Artikel  >  Backend-Entwicklung  >  请问一上文件上载时,HTTP响应消息头的有关问题

请问一上文件上载时,HTTP响应消息头的有关问题

WBOY
WBOYOriginal
2016-06-13 10:37:14819Durchsuche

请教一下文件下载时,HTTP响应消息头的问题。
我在学习利用PHP实现文件下载的时候,教材说需要提供三个http消息头:

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->    header("Content-type: application/octet-stream");          //高速浏览器传递的是文件流    header("Accept-Length: 2048");                             //文件大小    header("Content-Disposition: attachment; filename=abc.txt"); //指定文件名


其中,“Content-type”和“Content-Disposition”两个消息头,我还是有点不太理解,所以我分别做了两个例子:

a.php:
PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?php header("Content-type: application/octet-stream");    header("Accept-Length: 2048");        echo 123;


b.php:
PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?php header("Accept-Length: 2048");    header("Content-Disposition: attachment; filename=abc.txt");        echo 123;


我的问题是:
1、这两个页面都少一个消息头,为什么还都能成功的下载文件?
2、我虽然指定的文件大小是2048个字节,但是真实输出的只有“123”三个字节,为什么也能正常的下载文件?
“Accept-Length”消息头是可设可不设吗?

我刚刚学习http,麻烦大家就我问的问题回答,不要引出更深的概念,因为我看不懂。。
十分感谢!:)

------解决方案--------------------
1、这两个页面都少一个消息头,为什么还都能成功的下载文件?

因为你没有设定Content-Length,默认连接结束时的输出就是下载内容。

2、我虽然指定的文件大小是2048个字节,但是真实输出的只有“123”三个字节,为什么也能正常的下载文件?“Accept-Length”消息头是可设可不设吗?

Accept-Length的意思是最大可接受字符串的长度。所以可设可不设。
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn