【发布时间】:2021-04-13 16:50:15
【问题描述】:
我想下载一个文件并确定它的 MIME 类型。为此,我使用了axios 和file-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
inputargument to be of typeUint8ArrayorBufferorArrayBuffer, got `string``