Search code examples
node.jsaxiosmime

Downloading a file and determining MIME type with Node JS with axios, file-type


I want to download a file and determine its MIME type. For this, I am using axios and the file-type libraries.

Here's my code:

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
}

For some reason, type is always undefined here, no matter what the URL was.


Solution

  • The problem was actually related to axios. Instead of "arrayBuffer" use all lowercase: "arraybuffer"