下载一个没有文件结尾nodejs的文件

Download a file without file ending nodejs

例如:https://preview.redd.it/04o5k2n06zd71.jpg?width=960&crop=smart&auto=webp&s=7baa25e7e1bde3436cb2a265e2fc8c6cb9a758ee

我怎样才能将该图像下载/加载到缓冲区

注意: 结局总是不同的。

这是一个使用 got() 库的 nodejs 实现:

const got = require('got');

const url =
    'https://preview.redd.it/04o5k2n06zd71.jpg?width=960&crop=smart&auto=webp&s=7baa25e7e1bde3436cb2a265e2fc8c6cb9a758ee';

got(url, { responseType: 'buffer' }).then(result => {
    // result.body contains a Buffer object with the image in it
    console.log(`content-type: ${result.headers['content-type']}`);
    console.log(`isBuffer: ${Buffer.isBuffer(result.body)}`);
    console.log(result.body);
}).catch(err => {
    console.log(err);
});

您可以 运行 直接使用 nodejs 编写此代码(安装 got() 库后)。当我 运行 它时,它会生成此输出:

content-type: image/jpeg
isBuffer: true
<Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff e2 02 1c 49 43 43 5f 50 52 4f 46 49 4c 45 00 01 01 00 00 02 0c 6c 63 6d 73 02 10 00 00 ... 89335 more bytes>