1

詳細をファイルに追加して、ダウンロード用に追加しようとしています。

この目的のために JavaScript と PHP を使用しています。ダウンロード ボタンをクリックすると、AJAX 要求が起動されます。

$.ajax({
  url:"php/test.php",
  type: 'POST',
  data: { totalQuery : test1, },

  success: function(finalEntityList){
  },
});

test.php1行のコードがあると仮定しましょう

$html="Test";

これをファイルに追加して、ダウンロードできるようにします。私はコードを使用しました

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
$output = fopen('php://output', 'w');
fwrite($output, $html);
fclose($output);

しかし、ダウンロードは自動的に開始されません...ダウンロードが開始されるように、firebugを使用してPOSTリクエストリンクを開く必要があります..何が問題なのですか??

4

2 に答える 2

1

おそらく、AJAX 呼び出しでファイルのパスを返し、次のいずれかを使用して JavaScript を使用してダウンロードを「開始」するだけで済みます。

  • window.open
  • window.location.href
$.ajax({
  url:"php/test.php",
  type: 'POST',
  dataType: 'json',
  data: { totalQuery : test1, },

  success: function(response){
    // initiate download using direct path to file
    window.location.href = response.URL;
  }
});

これで、test.phpファイルはダウンロード ファイルの URL パスを JSON 形式で返すだけで済みます -

$filename = 'data.csv';
$path = $_SERVER['DOCUMENT_ROOT'].'/downloads/';
echo json_encode(array('URL'=>$path.$filename));

URL を生の文字列として返すことを検討するかもしれませんが、追加の解析関数を必要とせずに応答に追加情報を簡単に追加できるため、JSON を使用する方が良いと思います。これらすべてにより、より堅牢な選択になります。

于 2012-07-27T09:09:17.523 に答える
0

It requires some more parameters and headerinfo:

$file = "data.csv";
$mime_type = "text/csv";
$size = filesize($file);
$name = rawurldecode($name);

@ob_end_clean(); //turn off output buffering to decrease cpu usage

// required for IE, otherwise Content-Disposition may be ignored
if(ini_get('zlib.output_compression'))
    ini_set('zlib.output_compression', 'Off');

header('Content-Type: ' . $mime_type);
header('Content-Disposition: attachment; filename="'.$name.'"');
header("Content-Transfer-Encoding: binary");
header('Accept-Ranges: bytes');

/* The three lines below basically make the
download non-cacheable */
header("Cache-control: private");
header('Pragma: private');
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

// multipart-download and download resuming support
if(isset($_SERVER['HTTP_RANGE']))
{
    list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2);
    list($range) = explode(",",$range,2);
    list($range, $range_end) = explode("-", $range);
    $range=intval($range);
    if(!$range_end)
    {
        $range_end=$size-1;
    } 
    else
    {
        $range_end=intval($range_end);
    }

    $new_length = $range_end-$range+1;
    header("HTTP/1.1 206 Partial Content");
    header("Content-Length: $new_length");
    header("Content-Range: bytes $range-$range_end/$size");
} 
else
{
    $new_length=$size;
    header("Content-Length: ".$size);
}

/* output the file itself */
$chunksize = 1*(1024*1024); //you may want to change this
$bytes_send = 0;
if ($file = fopen($file, 'r'))
{
    if(isset($_SERVER['HTTP_RANGE']))
        fseek($file, $range);

    while(!feof($file) && (!connection_aborted()) && ($bytes_send<$new_length))
    {
        $buffer = fread($file, $chunksize);
        print($buffer); //echo($buffer); // is also possible
        flush();
        $bytes_send += strlen($buffer);
    }
    fclose($file);
} 
else 
    die('Error - can not open file.');
于 2012-07-27T09:24:30.243 に答える