[JS]如何下载图片到本地

1.直接下载 a标签加个download

2.asyne

async function downloadImage(imageSrc) {
                const image = await fetch(imageSrc);
                const imageBlog = await image.blob();
                const imageURL = URL.createObjectURL(imageBlog);

                const link = document.createElement("a");
                link.href = imageURL;
                link.download = "image file name here";
                document.body.appendChild(link);
                link.click();
                document.body.removeChild(link);
            }
            let img = "https://xxx.com/1.png";
            downloadImage(img);

3.nodejs

const fs = require("fs");

const https = require("https");

function saveImageToDisk(url, path) {
    let fullUrl = url;
    let localPath = fs.createWriteStream(path);
    let request = https
        .get(fullUrl, function (response) {
            response.pipe(localPath);
        })
        .on("error", function (err) {
            console.log(err);
        });
}
let img = "https://i0.hdslb.com/bfs/article/c546f8b88ec17c0424573d1e7945689ae2de6072.png@942w_progressive.webp";

saveImageToDisk(img, "./uploads/" + Date.now() + ".webp");

给TA充电
共{{data.count}}人
人已充电
编程

[WEB开发]打字机效果 | 可调速

2022-7-2 0:12:07

编程

[python]批量获取网址IP与归属地

2022-7-7 18:32:35

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
今日签到
搜索