JS fetch 展示PDF

2022-10-18  本文已影响0人  jw_fc89

示例只是教大家开始怎么做,路还是得大家自己走。采用 fetch 请求接口。仅仅是个示例,可以用 ajax、axios 等请求,这里只是教大家怎么请求blob文件。

function get() {
  const data = JSON.stringify({
    fileName: "八月发票.pdf",
  });
  fetch("你的接口", {
    headers: {
      "Content-Type": "application/json",
      Authorization: "bearer ",
    },
    responseType: "blob",
    params: data,
  })
    .then((res) => res.blob())
    .then((res) => {
      let file = new Blob([res], { type: "application/pdf" });

      const changeUrl = URL.createObjectURL(file);

      window.open(changeUrl, "__self");
    })
    .catch((err) => {
      console.table(err);
    });
}

get();
上一篇 下一篇

猜你喜欢

热点阅读