文件 在 javascript / Extjs 中将字节数组下载为文件
File download a byte array as a file in javascript / Extjs
在我的 Ext Js 解决方案中,我正在调用一个返回此 JSON 格式的服务
{"success":true,"filename":"spreadsheet.xlsx","file":[80,75,3,4,20,0,...(many more)]}
如何使用文件名和字节数组(文件)的内容制作文件下载对话框?
更新
所以我找到了这个位来开始下载
var a = window.document.createElement('a');
a.href = window.URL.createObjectURL(new Blob(data.file, { type: 'application/octet-stream' }));
a.download = data.filename;
// Append anchor to body.
document.body.appendChild(a)
a.click();
// Remove anchor from body
document.body.removeChild(a)
目前还不错
但是我得到的文件已损坏所以我怀疑我需要 Encode/Decode 文件变量?
我必须先将文件转换为 Uint8Array,然后再将其传递给 Blob
var arr = data.file;
var byteArray = new Uint8Array(arr);
var a = window.document.createElement('a');
a.href = window.URL.createObjectURL(new Blob([byteArray], { type: 'application/octet-stream' }));
a.download = data.filename;
// Append anchor to body.
document.body.appendChild(a)
a.click();
// Remove anchor from body
document.body.removeChild(a)
阅读此答案很有帮助
基于 Jepzen 的响应,我能够使用此技术从浏览器中的 AWS S3 下载文档。 +1 杰普森
s3.getObject(params, function(err, data) {
if (err === null) {
var arr = data.Body;
var byteArray = new Uint8Array(arr);
var a = window.document.createElement('a');
a.href = window.URL.createObjectURL(new Blob([byteArray], { type: 'application/octet-stream' }));
a.download = fName; //fName was the file name portion of the key what was passed in as part of the key value within params.
// Append anchor to body.
document.body.appendChild(a)
a.click();
// Remove anchor from body
document.body.removeChild(a)
} else {
result = 'failure'
console.log("Failed to retrieve an object: " + err);
}
});
在我的 Ext Js 解决方案中,我正在调用一个返回此 JSON 格式的服务
{"success":true,"filename":"spreadsheet.xlsx","file":[80,75,3,4,20,0,...(many more)]}
如何使用文件名和字节数组(文件)的内容制作文件下载对话框?
更新
所以我找到了这个位来开始下载
var a = window.document.createElement('a');
a.href = window.URL.createObjectURL(new Blob(data.file, { type: 'application/octet-stream' }));
a.download = data.filename;
// Append anchor to body.
document.body.appendChild(a)
a.click();
// Remove anchor from body
document.body.removeChild(a)
目前还不错
但是我得到的文件已损坏所以我怀疑我需要 Encode/Decode 文件变量?
我必须先将文件转换为 Uint8Array,然后再将其传递给 Blob
var arr = data.file;
var byteArray = new Uint8Array(arr);
var a = window.document.createElement('a');
a.href = window.URL.createObjectURL(new Blob([byteArray], { type: 'application/octet-stream' }));
a.download = data.filename;
// Append anchor to body.
document.body.appendChild(a)
a.click();
// Remove anchor from body
document.body.removeChild(a)
阅读此答案很有帮助
基于 Jepzen 的响应,我能够使用此技术从浏览器中的 AWS S3 下载文档。 +1 杰普森
s3.getObject(params, function(err, data) {
if (err === null) {
var arr = data.Body;
var byteArray = new Uint8Array(arr);
var a = window.document.createElement('a');
a.href = window.URL.createObjectURL(new Blob([byteArray], { type: 'application/octet-stream' }));
a.download = fName; //fName was the file name portion of the key what was passed in as part of the key value within params.
// Append anchor to body.
document.body.appendChild(a)
a.click();
// Remove anchor from body
document.body.removeChild(a)
} else {
result = 'failure'
console.log("Failed to retrieve an object: " + err);
}
});