[英]transmit xlsx data from php to javascript through json data structure
From PHP Side , I generate XLSX data through PHPExcel. 从PHP Side ,我通过PHPExcel生成XLSX数据。 I need to transfer the xlsx data through a JSON data structure, to get status, message information, and exchange data if so. 我需要通过JSON数据结构传输xlsx数据,以获取状态,消息信息并交换数据(如果有)。 I use base64 encoding to encode the xsl data into a string from the JSON data structure. 我使用base64编码将xsl数据编码为JSON数据结构中的字符串。
Here is the environment : 这是环境:
Server : PHP 5.6.0 / Apache Web Server 2.4.9 / Microsoft Windows 7 SP1 服务器 :PHP 5.6.0 / Apache Web服务器2.4.9 / Microsoft Windows 7 SP1
//XLSX data generation
$objWriter = PHPExcel_IOFactory::createWriter($xls,"Excel2007");
ob_start();
$objWriter->save("php://output");
$xlsData = ob_get_contents();
ob_end_clean();
//xlsData is encoded in "ISO-8859-1"
$xlsData = iconv("UTF-8", "ISO-8859-1", $xlsData);
$xlsData = base64_encode($xlsData);
//JSON data struture. To exchange data between Server side (PHP) and client side (javascript)
$opResult = {status:1, messageType: 2, message = 'XLSX successfully generated', data: null};
$opResult->data = $xlsData;
$opResult = json_encode($opResult);
header("Content-type: application/json; charset=utf-8");
echo $opResult;
From Javascript side , I use php.js function to decode base64 encoding and FileSaver.js to save xlsx data to local file 从Javascript方面 ,我使用php.js函数解码base64编码,并使用FileSaver.js将xlsx数据保存到本地文件
Client : Javascript provided by Firefox 31.0 / Windows 7 SP1 客户端 :Firefox 31.0 / Windows 7 SP1提供的Javascript
xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function(){
if( (xmlHttp.readyState == 4) && (xmlHttp.status == 200)){
var opResult = JSON.parse(xmlHttp.responseText);
var xlsData = base64_decode(opResult.data);
var blob = new Blob([xlsData], {type: "application/octet-stream;charset=utf-8"});
saveAs(blob, "report.xlsx");
}
}
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.send(params);
The problem : 问题 :
At PHP side, XLSX file can be opened. 在PHP端,可以打开XLSX文件。 At client side the xlsx data file can not be opened by Microsoft Excel. 在客户端,Microsoft Excel无法打开xlsx数据文件。 I get error message of corrupt file, ... 我收到损坏文件的错误消息,...
Base64 data sent by PHP side and received at javascript side are the same. PHP端发送的和javascript端接收的Base64数据相同。
Is someone has already met such a problem ? 有人已经遇到过这样的问题吗? Or is there anyone who used a such mechanism to exchange data between PHP and Javascript ? 还是有人使用过这种机制在PHP和Javascript之间交换数据?
Can anyone help me about the following interrogations: 有人可以帮助我以下询问吗:
From PHP side : 从PHP方面:
1.1 Is encoding to UTF-8 needed? 1.1是否需要编码为UTF-8?
Since UTF is the encoding character used at javascript side, I use iconv function, but without success. 由于UTF是javascript端使用的编码字符,因此我使用了iconv函数,但没有成功。 I obtained an unreadable xlsx file at javascript side 我在JavaScript端获得了无法读取的xlsx文件
1.2 Does base64 encoding can be assigned to only one data member of a JSON data structure: 1.2是否可以将base64编码仅分配给JSON数据结构的一个数据成员:
$opResult->status = 1; $opResult->messageType = 2; $opResult->message = 'XLSX successfully generated'; $opResult->data = base64_encode($xlsData);
1.3 Does json_encode function need option parameters? 1.3 json_encode函数是否需要选项参数?
//I tried json_encode($opResult, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
But always having same problem at client side 但是在客户端总是有同样的问题
From Javascript side : 从Javascript方面 :
2.1 I use an old version of base64_decode function , not the last one with decodeURIComponent(..). 2.1我使用的是base64_decode函数的旧版本,而不是使用带有解码URIComponent(..)的最后一个版本。
If I use the last base64_encode version, I get an error message : URI malformed. 如果使用最新的base64_encode版本,则会收到错误消息:URI格式错误。
2.2 Is FileSaver.js correctly used ? 2.2是否正确使用FileSaver.js?
var blob = new Blob([xlsData], {type: "application/octet-stream;charset=utf-8"}); saveAs(blob, "report.xlsx");
Thanks you for your help 谢谢你的帮助
The subject is closed using a workaround. 使用解决方法关闭了该主题。
From PHP side : 从PHP方面:
1.1 No need to encode in UTF-8 1.1无需使用UTF-8编码
1.2 binary data is set as URL data 1.2二进制数据设置为URL数据
url = "data:". $mimeType. ";base64,".base64_encode(xlsData);
1.3 no need to pass option parameters to json_encode function. 1.3无需将选项参数传递给json_encode函数。
So the code becomes: 因此,代码变为:
//XLSX data generation
$objWriter = PHPExcel_IOFactory::createWriter($xls,"Excel2007");
ob_start();
$objWriter->save("php://output");
$xlsData = ob_get_contents();
ob_end_clean();
//JSON data struture. To exchange data between Server side (PHP) and client side (javascript)
$opResult = {status:1, messageType: 2, message = 'XLSX successfully generated', data: null};
$opResult->data = "data:". $mimeType. ";base64,".base64_encode($xlsData);
$opResult = json_encode($opResult);
header("Content-type: application/json; charset=utf-8");
echo $opResult;
From javascript side: 从javascript方面:
No need to use blob and Filesaver.js API 无需使用Blob和Filesaver.js API
The code becomes: 代码变为:
xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function(){
if( (xmlHttp.readyState == 4) && (xmlHttp.status == 200)){
var opResult = JSON.parse(xmlHttp.responseText);
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
a.href = opResult.data;
a.click();
window.URL.revokeObjectURL(opResult.data);
}
}
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.send(params);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.