2

我的 Web 系统上有一个 CSV 导出文件,我正在尝试从客户端查询它。我从来没有遇到过任何问题,但出于某种原因,对于我的一位客户,返回的数据在正文之前有一个额外的 4 个字符,我无法确定它的来源!

HTTP/1.1 200 OK
Date: Mon, 17 Jun 2013 09:17:22 GMT
Server: Apache/2.2
Set-Cookie: username=xxx expires=Mon, 17-Jun-2013 11:17:22 GMT; path=/
Set-Cookie: password=xxx; expires=Mon, 17-Jun-2013 11:17:22 GMT; path=/
Content-Disposition: attachment; filename="xxx-Stock.csv"
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/csv

20b1
"code","dsc","cat","sub","whqc","qty"
..................

在上面,20b1 在我的身体之前。我已经通过相同的方式手动下载了文件,但在文件中看不到它。

The export code looks like:
header("Content-Disposition: attachment; filename=\"" . $file . "\"");
header("Content-type: text/csv");
$tmp["header"] = "\"code\",";
$tmp["header"].= "\"dsc\",";
$tmp["header"].= "\"cat\",";
$tmp["header"].= "\"sub\",";
$tmp["header"].= "\"whqc\",";
$tmp["header"].= "\"qty\"\n";
echo $tmp["header"];

for ($i = 1; $i <= $data["count"]; $i++ ) {
  $j = 1;
  while ( isset($data[$i][$j]) ) {
    $tmp["line"] = "\"" .  str_replace("\"", "\"\"", $data[$i]["code"]) . "\",";
    $tmp["line"].= "\"" .  str_replace("\"", "\"\"", $data[$i]["full_name"]) . "\",";
    $tmp["line"].= "\"" .  str_replace("\"", "\"\"", $data[$i]["prstkcat"]) . "\",";
    $tmp["line"].= "\"" .  str_replace("\"", "\"\"", $data[$i]["prsubcat"]) . "\",";
    $tmp["line"].= "\"" .  str_replace("\"", "\"\"", $data[$i][$j]["whqc"]) . "\",";
    $tmp["line"].= "\"" .  str_replace("\"", "\"\"", $data[$i][$j]["qty"]) . "\"\n";
    echo $tmp["line"];
    $j++;
  }
}

奇怪的是,它不会发生在我的其他客户身上,所以我想也许来自这个客户的数据有些奇怪。如果我注释掉一些$tmp["line"],它不会显示它。其他时候,如果我注释掉其中一行,则此代码会更改。

到底是什么东西可以把这个角色放在那里?!

更新:仅当服务器返回分块编码时才这样做。我将代码更改为以下代码,它现在可以工作(希望得到解释,因为这不是我的专业领域!)。

header("Content-Disposition: attachment; filename=\"" . $file . "\"");
header("Content-type: text/csv");

$tmp["header"] = "\"code\",";
$tmp["header"].= "\"dsc\",";
$tmp["header"].= "\"cat\",";
$tmp["header"].= "\"sub\",";
$tmp["header"].= "\"whqc\",";
$tmp["header"].= "\"qty\"\n";
$tmp["line"] = "";
for ($i = 1; $i <= $data["count"]; $i++ ) {
  $j = 1;
  while ( isset($data[$i][$j]) ) {
    $tmp["line"].= "\"" .  str_replace("\"", "\"\"", $data[$i]["code"]) . "\",";
    $tmp["line"].= "\"" .  str_replace("\"", "\"\"", $data[$i]["full_name"]) . "\",";
    $tmp["line"].= "\"" .  str_replace("\"", "\"\"", $data[$i]["prstkcat"]) . "\",";
    $tmp["line"].= "\"" .  str_replace("\"", "\"\"", $data[$i]["prsubcat"]) . "\",";
    $tmp["line"].= "\"" .  str_replace("\"", "\"\"", $data[$i][$j]["whqc"]) . "\",";
    $tmp["line"].= "\"" .  str_replace("\"", "\"\"", $data[$i][$j]["qty"]) . "\"\n";
    $j++;
  }
}
header("Content-Length: " . strlen($tmp["header"] . $tmp["line"]));
echo $tmp["header"] . $tmp["line"];
4

1 回答 1

0

删除这个

echo $tmp["header"];
于 2013-06-17T09:34:20.217 回答