[英]Is it possible to get print version of website as PDF in byte array using javascript
[英]Is it possible to convert byte array/stream to files such as Word,Excel, PDF in JavaScript
我有一个有关使用JavaScript进行文件操作的查询-
场景-我的JS函数调用wcf服务,该服务以字节数组或流以及mime类型的形式返回文件内容。 该字节数组/流需要转换为文件,并将其下载到用户的计算机上。
参考代码 -
var arr = "This is test content";
var byteArray = new Uint8Array(arr);
var a = window.document.createElement('a');
a.href = window.URL.createObjectURL(new Blob([byteArray], {
type: 'text/plain'
}));
a.download = "Test";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
该代码仅适用于文本文件。 MIME类型不是文本的文件已损坏。 我知道文件操作在客户端受到了严格限制,但只是为了确认-是否总有将字节数组/流转换为Word,Excel,PDF等文件的方法?
为此,我实现了类似的目标。 从您拥有byteArray的地方接起,然后尝试以下操作:
var byteArray = new Uint8Array(arrayBuffer);
var a = window.document.createElement('a');
a.href = window.URL.createObjectURL(new Blob([byteArray], { type:'application/octet-stream' }));
// supply your own fileName here...
a.download = "YourFileName.XLSX";
document.body.appendChild(a)
a.click();
document.body.removeChild(a)
将contentType设置为“ application / octet-stream”将适应任何二进制文件类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.