首页 > 解决方案 > 无法从文件名中探测 mimetype

问题描述

我正在从 azure 读取文件。并且 mimetypevideo/mp4response.headers["content-type"]. 我找不到如何将 mimetypes 添加到文件,因为我收到以下错误

无法从文件名探测 mimetype

axios({
  method: "get",
  url: "https://stemuli.blob.core.windows.net/stemuli/mentor-lesson-video-c20665e2-b17c-4f22-9ef8-f6cd62b113dd.mp4",
  responseType: "stream"
}).then(function (response) {
  const videoType = response.headers["content-type"].split("/")[1];

  const file = fs.createWriteStream(
    `./cache/thumbnails/${tempFileName + "."}${videoType}`
  );
  response.data.pipe(file);

  thumbsupply
    .generateThumbnail(`./cache/thumbnails/${tempFileName}`)
    .then(thumb => res.json(thumb))
    .catch(err => {
      res.json({
        Error: "Error creating thumbnail for video"
      });
      console.log(err);
    });
});

标签: javascriptnode.js

解决方案


您收到的错误来自在线的 thumbsupply 包:

 thumbsupply
    .generateThumbnail(`./cache/thumbnails/${tempFileName}`)

您可以在他们的代码中看到错误消息

    _fetchThumbnailSupplier(file, options) {
        const mime = options.mimetype || mimetypes.lookup(file);
        let Supplier;

        if (!mime) {
            throw new E.UnknownFiletypeError(file, undefined, "Unable to probe mimetype from filename");
        }

因此,您只需传入 mimetype 选项即可更正您的代码:

  thumbsupply
    .generateThumbnail(`./cache/thumbnails/${tempFileName}`,
        { mimetype: response.headers["content-type"] } )

推荐阅读