2012-07-27 89 views
1

我正试图在文件中附加一些细节并添加下载。使用AJAX调用下载文件

我正在使用JavaScript和PHP来达到这个目的。点击下载按钮,它会触发一个AJAX请求。

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

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

让我们假设test.php有一行代码

$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); 

但下载将不会启动automatcially ......我打开使用萤火,使下载将被启动POST请求链接..什么可能是错误的?

+1

把你的文件名在引号'标题(“内容处置:附件;文件名=‘data.csv’”);' – 2012-07-27 09:11:15

+0

是力做的伎俩 – user1371896 2012-07-27 09:13:45

+0

@jav - 如果你要编辑一篇文章 - 尝试修复** ALL **的问题 - [不只是一两个错误](http://stackoverflow.com/posts/11684714/revisions)。 – Lix 2012-07-27 09:23:18

回答

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文件将只需要返回的下载文件中的一个JSON格式的URL路径 -

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

你可能会考虑返回URL作为原始字符串 - 但我觉得使用JSON可能是因为您更好可以轻松地将其他信息添加到响应中,而无需额外的解析功能。所有这些使得它成为更强大的选择。

+0

林不知道,bt什么可能是使用php下载文件时的默认路径 – user1371896 2012-07-27 09:11:17

+0

路径就是表示文件的URL。如果该文件位于您的Web根文件夹中,那么它的简单您的站点名称和文件名称 - 'http:// yourdomain.com/data.csv' – Lix 2012-07-27 09:13:19

+0

该路径是脚本所在的位置,因此如果脚本处于' http:// yourdomain.com/script/download.php'那么该脚本的文件根目录是'http:// yourdomain.com/script /' – 2012-07-27 09:25:53

0

它需要更多的参数和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.'); 
+0

JavaScript方面呢?这看起来像它会启动一个文件下载 - 但是当它由jQuery AJAX调用启动时它将如何表现? – Lix 2012-07-27 09:26:06

+0

fwrite部分在哪里完成? – user1371896 2012-07-27 09:26:58

+0

no it doesnt ..... – user1371896 2012-07-27 09:29:53