对于超过 2.5K 的记录,在 chrome 中下载/导出失败
Downloading / Exporting fails in chrome for records exceeding 2.5K
在 OpenCart 框架中
- 点击导出按钮后,所有客户选择的产品列表目录将包含所有数据导出。
- 此处出现“下载选项”按钮
点击下载后,根据记录,它会在 chrome 以及 firefox 浏览器中以 CSV 格式成功下载。
对于超过 2.5K 的记录,
- 尤其是使用 FireFox 浏览器时,大约 5K 条记录需要花费太多时间,需要 15 分钟才能成功执行和下载。
- 但在 Chrome 的情况下,下载失败 - 网络错误
来自我的控制器文件:
function download(){
$export_type = $this->request->post['export_type'];
if($export_type=='p'){
$customer_id = $this->customer->getId();
$this->load->model('account/wishlist');
$this->load->model('catalog/product');
$noofproducts = $this->model_account_wishlist->getTotalWishlist();
$data = array();
ob_start();
$product_ids = $this->model_account_wishlist->getProductIdFromWishList();
$products = $this->model_account_wishlist->getProduct($product_ids);
$filename = "data_export_" . date("Y-m-d") . ".csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
header("Pragma: no-cache");
header("Expires: 0");
$result = $this->ExportCSVFile($products) ;
$xlsData = ob_get_contents();
$length = ob_get_length();
header('Content-Disposition: attachment; filename='.$filename);
$response = array(
'op' => 'ok',
'file' => "data:text/csv;base64,".base64_encode($xlsData),
'filename' => $filename
);
ob_get_clean();
ob_end_flush();
die(json_encode($response));
}
}
function ExportCSVFile($records) {
// create a file pointer connected to the output stream
$fh = fopen( 'php://output', 'w' );
$heading = false;
if(!empty($records))
foreach($records as $row) {
if(!$heading) {
// output the column headings
fputcsv($fh, array_keys($row));
$heading = true;
}
// loop over the rows, outputting them
fputcsv($fh, array_values($row));
}
fclose($fh);
}
这是我在 Ajax 脚本中的视图文件 (tpl):
<script type="text/javascript">
function getNotifications() {
$('#export_import_notification').html('<i class="fa fa-info-circle"></i><button type="button" class="close" data-dismiss="alert">×</button> <div id="export_import_loading"><img src="view/image/export-import/loading.gif" /><?php echo $text_loading_notifications; ?></div>');
setTimeout(
function(){
$.ajax({
type: 'GET',
url: 'index.php?route=account/wishlist/getNotifications&token=<?php echo $token; ?>',
dataType: 'json',
success: function(json) {
if (json['error']) {
$('#export_import_notification').html('<i class="fa fa-info-circle"></i><button type="button" class="close" data-dismiss="alert">×</button> '+json['error']+' <span style="cursor:pointer;font-weight:bold;text-decoration:underline;float:right;" onclick="getNotifications();"><?php echo $text_retry; ?></span>');
} else if (json['message']) {
$('#export_import_notification').html('<i class="fa fa-info-circle"></i><button type="button" class="close" data-dismiss="alert">×</button> '+json['message']);
} else {
$('#export_import_notification').html('<i class="fa fa-info-circle"></i><button type="button" class="close" data-dismiss="alert">×</button> '+'<?php echo $error_no_news; ?>');
}
},
failure: function(){
$('#export_import_notification').html('<i class="fa fa-info-circle"></i><button type="button" class="close" data-dismiss="alert">×</button> '+'<?php echo $error_notifications; ?> <span style="cursor:pointer;font-weight:bold;text-decoration:underline;float:right;" onclick="getNotifications();"><?php echo $text_retry; ?></span>');
},
error: function() {
$('#export_import_notification').html('<i class="fa fa-info-circle"></i><button type="button" class="close" data-dismiss="alert">×</button> '+'<?php echo $error_notifications; ?> <span style="cursor:pointer;font-weight:bold;text-decoration:underline;float:right;" onclick="getNotifications();"><?php echo $text_retry; ?></span>');
}
});
},
500
);
}
我们需要从 chrome 导入证书吗?
解决是由于服务器上域名服务器的代理设置
边.
尝试以下步骤:
- 从 IPv4/v6 设置中删除/禁用任何 DNS 服务
- 不要使用任何代理服务器(HTTP、SOCKS v4/v5)
- 禁用 DNS 预取
- 清除浏览器缓存(临时 Internet 文件)
在 OpenCart 框架中
- 点击导出按钮后,所有客户选择的产品列表目录将包含所有数据导出。
- 此处出现“下载选项”按钮
点击下载后,根据记录,它会在 chrome 以及 firefox 浏览器中以 CSV 格式成功下载。
对于超过 2.5K 的记录,
- 尤其是使用 FireFox 浏览器时,大约 5K 条记录需要花费太多时间,需要 15 分钟才能成功执行和下载。
- 但在 Chrome 的情况下,下载失败 - 网络错误
来自我的控制器文件:
function download(){
$export_type = $this->request->post['export_type'];
if($export_type=='p'){
$customer_id = $this->customer->getId();
$this->load->model('account/wishlist');
$this->load->model('catalog/product');
$noofproducts = $this->model_account_wishlist->getTotalWishlist();
$data = array();
ob_start();
$product_ids = $this->model_account_wishlist->getProductIdFromWishList();
$products = $this->model_account_wishlist->getProduct($product_ids);
$filename = "data_export_" . date("Y-m-d") . ".csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
header("Pragma: no-cache");
header("Expires: 0");
$result = $this->ExportCSVFile($products) ;
$xlsData = ob_get_contents();
$length = ob_get_length();
header('Content-Disposition: attachment; filename='.$filename);
$response = array(
'op' => 'ok',
'file' => "data:text/csv;base64,".base64_encode($xlsData),
'filename' => $filename
);
ob_get_clean();
ob_end_flush();
die(json_encode($response));
}
}
function ExportCSVFile($records) {
// create a file pointer connected to the output stream
$fh = fopen( 'php://output', 'w' );
$heading = false;
if(!empty($records))
foreach($records as $row) {
if(!$heading) {
// output the column headings
fputcsv($fh, array_keys($row));
$heading = true;
}
// loop over the rows, outputting them
fputcsv($fh, array_values($row));
}
fclose($fh);
}
这是我在 Ajax 脚本中的视图文件 (tpl):
<script type="text/javascript">
function getNotifications() {
$('#export_import_notification').html('<i class="fa fa-info-circle"></i><button type="button" class="close" data-dismiss="alert">×</button> <div id="export_import_loading"><img src="view/image/export-import/loading.gif" /><?php echo $text_loading_notifications; ?></div>');
setTimeout(
function(){
$.ajax({
type: 'GET',
url: 'index.php?route=account/wishlist/getNotifications&token=<?php echo $token; ?>',
dataType: 'json',
success: function(json) {
if (json['error']) {
$('#export_import_notification').html('<i class="fa fa-info-circle"></i><button type="button" class="close" data-dismiss="alert">×</button> '+json['error']+' <span style="cursor:pointer;font-weight:bold;text-decoration:underline;float:right;" onclick="getNotifications();"><?php echo $text_retry; ?></span>');
} else if (json['message']) {
$('#export_import_notification').html('<i class="fa fa-info-circle"></i><button type="button" class="close" data-dismiss="alert">×</button> '+json['message']);
} else {
$('#export_import_notification').html('<i class="fa fa-info-circle"></i><button type="button" class="close" data-dismiss="alert">×</button> '+'<?php echo $error_no_news; ?>');
}
},
failure: function(){
$('#export_import_notification').html('<i class="fa fa-info-circle"></i><button type="button" class="close" data-dismiss="alert">×</button> '+'<?php echo $error_notifications; ?> <span style="cursor:pointer;font-weight:bold;text-decoration:underline;float:right;" onclick="getNotifications();"><?php echo $text_retry; ?></span>');
},
error: function() {
$('#export_import_notification').html('<i class="fa fa-info-circle"></i><button type="button" class="close" data-dismiss="alert">×</button> '+'<?php echo $error_notifications; ?> <span style="cursor:pointer;font-weight:bold;text-decoration:underline;float:right;" onclick="getNotifications();"><?php echo $text_retry; ?></span>');
}
});
},
500
);
}
我们需要从 chrome 导入证书吗?
解决是由于服务器上域名服务器的代理设置
边.
尝试以下步骤:
- 从 IPv4/v6 设置中删除/禁用任何 DNS 服务
- 不要使用任何代理服务器(HTTP、SOCKS v4/v5)
- 禁用 DNS 预取
- 清除浏览器缓存(临时 Internet 文件)