content-disposition 在响应 cURL 请求时使用什么?
What content-disposition to use when giving response to a cURL request?
假设我们有一个客户端(实际上是服务器端 PHP 脚本)和一个服务器(也是服务器端 PHP 脚本)。
客户端通过 cURL
向服务器发出 HTTP 请求。客户端接受text/csv
,因此设置了相应的header,并且客户端希望将响应保存到文件中,因此正确设置了CURLOPT_FILE
选项。
问题是服务器在处理请求并返回 CSV "encoded" 内容时,是否应该使用 inline
或 attachment
作为 [=18] 的值=] header?
一个非常简单的pseudo-code测试:服务器做这样的事情:
if (file_exists($attachment_location)) {
header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
header("Cache-Control: public"); // needed for internet explorer
header("Content-Type: text/csv");
header("Content-Length:".filesize($attachment_location));
header("Content-Disposition: inline");
readfile($attachment_location);
die();
} else {
header($_SERVER["SERVER_PROTOCOL"] . " 404 Not found");
}
或者应该是这样的:
<?php
$attachment_location = "./c3m.csv";
if (file_exists($attachment_location)) {
header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
header("Cache-Control: public"); // needed for internet explorer
header("Content-Type: text/csv");
header("Content-Transfer-Encoding: Binary");
header("Content-Length:".filesize($attachment_location));
header("Content-Disposition: attachment; filename=c3m.csv");
readfile($attachment_location);
die();
} else {
header($_SERVER["SERVER_PROTOCOL"] . " 404 Not found");
}
cURL 不关心 Content-Disposition,因此它应该与您赋予它的值无关。
假设我们有一个客户端(实际上是服务器端 PHP 脚本)和一个服务器(也是服务器端 PHP 脚本)。
客户端通过 cURL
向服务器发出 HTTP 请求。客户端接受text/csv
,因此设置了相应的header,并且客户端希望将响应保存到文件中,因此正确设置了CURLOPT_FILE
选项。
问题是服务器在处理请求并返回 CSV "encoded" 内容时,是否应该使用 inline
或 attachment
作为 [=18] 的值=] header?
一个非常简单的pseudo-code测试:服务器做这样的事情:
if (file_exists($attachment_location)) {
header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
header("Cache-Control: public"); // needed for internet explorer
header("Content-Type: text/csv");
header("Content-Length:".filesize($attachment_location));
header("Content-Disposition: inline");
readfile($attachment_location);
die();
} else {
header($_SERVER["SERVER_PROTOCOL"] . " 404 Not found");
}
或者应该是这样的:
<?php
$attachment_location = "./c3m.csv";
if (file_exists($attachment_location)) {
header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
header("Cache-Control: public"); // needed for internet explorer
header("Content-Type: text/csv");
header("Content-Transfer-Encoding: Binary");
header("Content-Length:".filesize($attachment_location));
header("Content-Disposition: attachment; filename=c3m.csv");
readfile($attachment_location);
die();
} else {
header($_SERVER["SERVER_PROTOCOL"] . " 404 Not found");
}
cURL 不关心 Content-Disposition,因此它应该与您赋予它的值无关。