【问题标题】:Downloading a file and determining MIME type with Node JS with axios, file-type使用带有 axios、文件类型的 Node JS 下载文件并确定 MIME 类型
【发布时间】:2021-04-13 16:50:15
【问题描述】:

我想下载一个文件并确定它的 MIME 类型。为此,我使用了axiosfile-type 库。

这是我的代码:

async function download(url) {
    const response = await axios.get(url, {
        responseType: "arrayBuffer"
    });
    const buffer = Buffer.from(response.data, "binary");
    console.log(buffer); // <Buffer fd fd fd fd 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 fd fd 00 fd 00 09 09 0a 08 0a 08 0b 0b 09 0b 0a 0b 0b 0b 0e 10 0c 0a 0b 0d 13 17 15 10 14 ... 54078 more bytes>

    const type = await FileType.fromBuffer(buffer);
    console.log(type); // undefined
}

出于某种原因,type 在此处始终为 undefined,无论 URL 是什么。

【问题讨论】:

  • 你试过 FileType.fromBuffer(response.data) 吗?
  • @ducmai 这给了我:TypeError: Expected the input argument to be of type Uint8Array or Buffer or ArrayBuffer, got `string``

标签: node.js axios mime


【解决方案1】:

问题实际上与axios有关。而不是 "arrayBuffer" 使用全部小写:"arraybuffer"

【讨论】:

    猜你喜欢
    • 2014-08-31
    • 2010-11-07
    • 2013-04-21
    • 2011-08-25
    • 2013-02-20
    • 2016-05-23
    • 2011-01-28
    • 2011-11-09
    • 1970-01-01
    相关资源
    最近更新 更多