Javascript 使用 JSZip 将 blob 转换为图像
Javascript blob to image converting with JSZip
var bl = window.URL.createObjectURL(xhr.response)
var zip = new JSZip();
zip.file(bl);
zip.generateAsync({type:"blob"})
.then(function(content) {
saveAs(content, "example.zip");
}, function(err){
console.log(err)
})
我的 XmlHttpRequest 从图像文件中得到了 'blob' 类型的响应。如何将 blob 图像文件转换为图像文件(可以是 .gif、.jpg、.bmp、.jpg-large 等),以便制作一个没有错误的 zip 文件?
使用 URL.createObjectURL
可以得到 blob 的 url(例如 blob:https://whosebug.com/e62c177a-b4b1-4945-8e13-53bb5a3c8f34
)。 JSZip 无法解析它,但您可以直接使用 blob(因此在您的情况下为 xhr.response
)。正如 Patrick Evans 在评论中所说,您还需要提供文件名。
var zip = new JSZip();
zip.file("my_file.ext", xhr.response);
zip.generateAsync({type:"blob"})
.then(function(content) {
saveAs(content, "example.zip");
}, function(err){
console.log(err)
});
var bl = window.URL.createObjectURL(xhr.response)
var zip = new JSZip();
zip.file(bl);
zip.generateAsync({type:"blob"})
.then(function(content) {
saveAs(content, "example.zip");
}, function(err){
console.log(err)
})
我的 XmlHttpRequest 从图像文件中得到了 'blob' 类型的响应。如何将 blob 图像文件转换为图像文件(可以是 .gif、.jpg、.bmp、.jpg-large 等),以便制作一个没有错误的 zip 文件?
使用 URL.createObjectURL
可以得到 blob 的 url(例如 blob:https://whosebug.com/e62c177a-b4b1-4945-8e13-53bb5a3c8f34
)。 JSZip 无法解析它,但您可以直接使用 blob(因此在您的情况下为 xhr.response
)。正如 Patrick Evans 在评论中所说,您还需要提供文件名。
var zip = new JSZip();
zip.file("my_file.ext", xhr.response);
zip.generateAsync({type:"blob"})
.then(function(content) {
saveAs(content, "example.zip");
}, function(err){
console.log(err)
});