javascript - 错误: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');
});
},
解决方案
推荐阅读
- batch-file - 7Z SFX 未知发行者
- php - 如何通过对一个数据表的多个查询获取多个表数据
- c++ - OpenGL,纹理输出为纯色
- javascript - Reducer 不断返回一个空数组
- mongodb - [StitchServiceError: Error: ObjectId in 必须是 12 个字节的单个字符串或 24 个十六进制字符的字符串]
- replace - 替代:替换评估
- squarespace - 索引页的 Squarespace 索引页
- delphi - AllocConsole、SetConsoleCtrlHandler 防止终止
- python - 是否有通过列表替换值的功能?
- html - 获取错误标签以与 flex-wrap 中的输入对齐