Vue 跨域下载或读取文件

160 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

image.png

  • 下载文件
export async function download(file_url, file_name) {
  let res = await Axios({
    method: "get",
    url: file_url,
    responseType: "blob"
  });
  let newUrl = window.URL.createObjectURL(res.data);
  let a = document.createElement("a");
  a.href = newUrl;
  a.download = file_name;
  a.click();
  a.remove();
  window.URL.revokeObjectURL(newUrl);
}
  • 读取文件
export async function read(text_url) {
  let res = await Axios({
    method: "get",
    url: text_url,
    responseType: "text"
  });
  return res.data
}