Javascript 添加图像后 JSZip 出现错误

Javascript JSZip having error after adding image

我想下载一些 tampermonkey 脚本,帮助我从网站下载图像,然后打包为 zip 文件供我下载。我使用 JSZip 来打包图像。

我按照示例代码没有问题,但是当添加图像时,我会收到以下错误:

UnhandledPromiseRejectionWarning: Error: Invalid base64 input, it looks like a data url.

我把我的代码放在 Runkit 中,如果第 19 行有注释(没有将下载的图像添加到 zip 文件中)一切都会 运行 顺利。我不确定如何将图像添加到 zip 文件中。

提前致谢

问题的根本原因是下载的图片不符合base64要求。 JSZip 部分没有问题。

以下是正确下载图片的不同方法:

Node

I managed to replace the image downloading library from request to node-base64-image and everything is working now. I guess you can still use request but need to tune the parameters in order to get that work. Here is the working Runkit. So in conclusion the problem is on download side.

Browser

As I want to download the image from the using a browser script (tampermonkey) so the node solution doesn't fit me. After searching a bit I find a working solution:

        GM_xmlhttpRequest({
            method: "GET",
            url: url,
            headers: { referer: url, origin: url },
            responseType: 'blob',
            onload: response => {
                // Here the response.response would be the image that works for JSZip
            }
        });