DRUPAL 7:触发来自 Ajax 调用的下载请求
DRUPAL 7: Trigger Download request from Ajax call
生成和触发下载对话框的方法不正确。无法从后台进程启动对话(下载)(AJAX 调用)
我正在尝试 create/download 单击按钮时的 CSV 文件。当我只是尝试创建 CSV 时,代码工作正常,如下所示:
Drupal 按钮:
$form['Export'] = array(
'#type' => 'submit',
'#value' => t('Export'),
"#ajax" => array(
"callback" => "export_csv",
),
处理代码:
$out = fopen('filename.csv', 'w');
//processing to create file csv
fputcsv($out, $info, ";");
fclose($out);
csv 文件已创建并存储在根目录中。
然而,当我尝试添加 headers 时,下面的代码失败并出现 ajax 错误并且没有调试信息
$fichier = 'inscriptions.csv';
header( "Content-Type: text/csv;charset=utf-8" );
header( "Content-Disposition: attachment;filename=\"$fichier\"" );
header("Pragma: no-cache");
header("Expires: 0");
$out = fopen('php://output', 'w');
//processing to create file csv
fputcsv($out, $info, ";");
fclose($out);
如@misorude 评论中所述 - 不要尝试从后台请求触发下载,而是使其成为 drupal 方式。
以免假设您有 my_export_module。
在hook_menu
//(...)
//export download csv
$items['export/download-csv'] = array(
'page callback' => 'my_export_module_download_csv',
'delivery callback' => 'my_export_module_deliver_csv',
'type' => MENU_CALLBACK,
);
在my_export_module_download_csv
函数中。假设 $input
是要导出的二维数组。
//(...)
//generate csv
//open tmp stream
$f = fopen('php://temp', 'w');
foreach ($input as $input_line) {
if (is_array($input_line)) {
fputcsv($f, $input_line, $delimiter);
}
}
fclose($f);
return array(
'name' => $output_file_name,
);
最后 my_export_module_deliver_csv
函数
function my_export_module_deliver_csv($var = NULL) {
drupal_add_http_header('Content-Encoding', 'UTF-8');
drupal_add_http_header('Content-Type', 'application/csv;charset=UTF-8');
if (isset($var['name'])) {
drupal_add_http_header('Content-Disposition', 'attachment; filename="' . $var['name'] . '";');
}
if (isset($var['file'])) {
echo $var['file'];
}
}
这种方式文件不会存储在服务器上,但是当输入export/download-csv时应该会触发下载。
生成和触发下载对话框的方法不正确。无法从后台进程启动对话(下载)(AJAX 调用)
我正在尝试 create/download 单击按钮时的 CSV 文件。当我只是尝试创建 CSV 时,代码工作正常,如下所示:
Drupal 按钮:
$form['Export'] = array(
'#type' => 'submit',
'#value' => t('Export'),
"#ajax" => array(
"callback" => "export_csv",
),
处理代码:
$out = fopen('filename.csv', 'w');
//processing to create file csv
fputcsv($out, $info, ";");
fclose($out);
csv 文件已创建并存储在根目录中。
然而,当我尝试添加 headers 时,下面的代码失败并出现 ajax 错误并且没有调试信息
$fichier = 'inscriptions.csv';
header( "Content-Type: text/csv;charset=utf-8" );
header( "Content-Disposition: attachment;filename=\"$fichier\"" );
header("Pragma: no-cache");
header("Expires: 0");
$out = fopen('php://output', 'w');
//processing to create file csv
fputcsv($out, $info, ";");
fclose($out);
如@misorude 评论中所述 - 不要尝试从后台请求触发下载,而是使其成为 drupal 方式。
以免假设您有 my_export_module。
在hook_menu
//(...)
//export download csv
$items['export/download-csv'] = array(
'page callback' => 'my_export_module_download_csv',
'delivery callback' => 'my_export_module_deliver_csv',
'type' => MENU_CALLBACK,
);
在my_export_module_download_csv
函数中。假设 $input
是要导出的二维数组。
//(...)
//generate csv
//open tmp stream
$f = fopen('php://temp', 'w');
foreach ($input as $input_line) {
if (is_array($input_line)) {
fputcsv($f, $input_line, $delimiter);
}
}
fclose($f);
return array(
'name' => $output_file_name,
);
最后 my_export_module_deliver_csv
函数
function my_export_module_deliver_csv($var = NULL) {
drupal_add_http_header('Content-Encoding', 'UTF-8');
drupal_add_http_header('Content-Type', 'application/csv;charset=UTF-8');
if (isset($var['name'])) {
drupal_add_http_header('Content-Disposition', 'attachment; filename="' . $var['name'] . '";');
}
if (isset($var['file'])) {
echo $var['file'];
}
}
这种方式文件不会存储在服务器上,但是当输入export/download-csv时应该会触发下载。