下载文件并使用 Node JS 和 axios, file-type 确定 MIME 类型

问题描述 投票:0回答:2

我想下载一个文件并确定其 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
}

出于某种原因,无论 URL 是什么,这里的

type
始终是
undefined

node.js axios mime
2个回答
1
投票

问题其实与

axios
有关。使用全小写代替
"arrayBuffer"
"arraybuffer"


0
投票

嘿,我有一个与此相关的问题。实际上我想下载一个基于 js 中的 mime 类型的文件。你有什么线索吗?

import React from "react";
import Loader from "../../../utilities/loader";
import { useFallbacksContext } from "../../../context/contentProvider/FallbacksContext";
import ImageComponent from "../../ImageComponent";
import webServiceAgent from "../../../api/webServiceAgent";

const ViewThisPolicyTabsPolicyDocuments = ({ policyDocs, isLoading, policyDataToPass }) => {
  const { myPolicyContentVariables } = useFallbacksContext();
  const downloadDocument = (handleString, mimeType, typeName) => {
    var docInfo = {
      handleString,
      mimeType,
      typeName,
    };
    webServiceAgent.PolicyDashboard.downloadFullfillment(docInfo, {
      responseType: "blob",
    })
      .then((response) => {
        console.log("success calling downloadFullfillment");
        const blob = new Blob([response], { type: "application/pdf" });

        const url = window.URL.createObjectURL(blob);
        const link = document.createElement("a");
        link.href = url;
        link.download = typeName;
        console.log(link);
        link.click();

        window.URL.revokeObjectURL(url);
      })
      .catch((error) => {
        console.error(error);
      });
      console.log("success calling downloadFullfillment",policyDocs);
  };

  return (
    <div className="policyDocuments ">
      {isLoading ? (
        <div className=" " style={{ minHeight: "40vh", width: "100%" }}>
          <Loader />
        </div>
      ) : (
        <div className="tabInfoContent">
          <div className="docsWrapper row mb-4 gap-4 ">
            <div className="image-section col-auto d-none d-md-inline viewIconsWrapper">
              <ImageComponent src={myPolicyContentVariables?.documentsIcon} alt="Documents" className="viewIcons" />
            </div>
            <div className="col">
              <div className="table-responsive">
                <table className="table table-borderless">
                  <tbody className="row">
                    {policyDocs[policyDataToPass?.policyNumber]?.map((item, index) => (
                      <tr
                        key={index}
                        className="d-flex flex-column flex-sm-row align-content-end m-0 p-0 justify-content-between px-3 my-2 align-items-center thead">
                        <td className="p-2 ps-0 pe-5">
                          <div className="d-flex flex-column flex-sm-row align-content-end">
                            <div className="fontOmnesSemiBold textBlueVar mb-1 mb-sm-0 ps-3 ">{item?.typeName || "-"}</div>
                          </div>
                        </td>
                        <td className="p-2">
                          <div className="fontOmnesMedium textBlueVar mb-1 mb-sm-0">
                            <button className="btn textWhite bgBlue" onClick={() => downloadDocument(item?.handleString, item?.mimeType, item?.typeName)}>
                              {myPolicyContentVariables?.downloadButtonLabel}
                            </button>
                          </div>
                        </td>
                      </tr>
                    ))}
                  </tbody>
                </table>
              </div>
            </div>
          </div>
        </div>
      )}
    </div>
  );
};

export default ViewThisPolicyTabsPolicyDocuments;

© www.soinside.com 2019 - 2024. All rights reserved.