我知道我有一个非常漂亮的缓冲区,如果直接写入文件,它会给我一个可接受的 zip 文件:
fs.writeFile("acceptable.zip", prettyBuffer);
//file acceptable.zip is a valid zip file
我怎样才能为用户提供这个非常漂亮的缓冲区作为下载?
我试过了
var uriContent = "data:application/zip," + prettyBuffer);
window.open(uriContent)
和
var uriContent = "data:application/octet-stream," + prettyBuffer);
window.open(uriContent)
并且至少有 10 种不同编码的变体,它仍然无法工作!
编辑:
这是我的代码
var AdmZip = require('adm-zip');
var zip = new AdmZip();
zip.addFile("row0", new Buffer("hi"), "comment");
var prettyBuffer = zip.toBuffer()
var uriContent = "data:application/zip;base64," + prettyBuffer.toString('base64');
var encodedUri = uriContent;
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "acceptable.zip");
link.click();