首页 > 解决方案 > 错误:ENOENT,当我使用 nodejs 下载 .jpg 时没有这样的文件或目录

问题描述

我正在使用nodejs服务器和discordjs构建一个discord bot,我想做的一件事是传递一个.jpg discord链接(即https://cdn.discordapp.com/attachments/537413376389939223/593608396113051653/ GiveEmTheWorm.png ) 到 npm 模块 jpeg-js,这样我就有希望检索特定像素的颜色。

我试图通过下载位于上面链接中的图像来解决这个问题。我也使用了其他方法,但它们都导致了各种不同的错误。不幸的是,其他具有类似错误的 stackoverflow 主题没有包含任何对我有帮助的内容。如果有人有更好的方式在 nodejs 中下载图像和/或更智能的方式来获取像素的颜色,请告诉我!

//attempt to download 
let download = function(uri, filename, callback){
request.head(uri, function(err, res, body){
    console.log('content-type:', res.headers['content-type']);
    console.log('content-length:', res.headers['content-length']);
    request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
    });
};
//the actual function call
download(`[the path above]`, 'file.jpg', function(){
 console.log('done');
});

//all that above so that I can hopefully do
let jpegData = fs.readFileSync('file.jpg');
let rawImageData = jpeg.decode(jpegData);
console.log(rawImageData);

//which will (in theory) allow me to inspect specific pixels.

预期结果:我希望将文件下载到该程序的根目录中,以便它的辅助部分可以将 jpeg 解码为我可以修改的内容。 https://imgur.com/a/fH7F6tk


编辑:我仍然遇到同样的错误,所以这是我更新的代码(我把片段做得更大了)

    let download = function(uri, filename, callback){
      request.head(uri, function(err, res, body){
         console.log('content-type:', res.headers['content-type']);
         console.log('content-length:', res.headers['content-length']);
         request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
         });
    };

    someOtherFunction()
    {
       let pokePath = 'thisisthebiglinkabove'
        console.log(pokePath);
        download(pokePath, 'nameoffile.jpg', function(){
          let jpegData = fs.readFileSync(filename);
          let rawImageData = jpeg.decode('nameoffile.jpg');
          console.log(rawImageData);
          console.log('done');
        });
    },

https://imgur.com/a/503mjyN

标签: javascriptnode.jsdiscord.js

解决方案


推荐阅读