前端下载文件并设置文件名字

data 是接口地址
   if (!data) {
        return;
      }
      let blob = new Blob([data], {
        type:
          "application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8"
      });
      let url = window.URL.createObjectURL(blob);
      let fileName = names + ".xlsx";
      if ("download" in document.createElement("a")) {
        const a = document.createElement("a");
        a.href = url;
        a.download = fileName;
        a.style.display = "none";
        document.body.appendChild(a);
        a.click();
        URL.revokeObjectURL(a.href);
        document.body.removeChild(a);
      } else {
        navigator.msSaveBlob(blob, fileName);
      }

你可能感兴趣的:(前端下载文件并设置文件名字)