本网站可以通过分类标签帮助你快速筛选出你想看的文章,记住地址:www.Facec.cc

前端下载文件并自定义名称

    async downLoadFile(row){
      let filename = '' + row.fileName  + "-" + row.company
      const blob = await this.getBlob(this.envUr + row.filePath);
      // this.saveAs(blob, filename);
      const link = document.createElement('a');
      const body = document.body;
      link.href = window.URL.createObjectURL(blob);
      link.download = filename;
      link.style.display = 'none';
      body.appendChild(link);
      link.click();
      body.removeChild(link);
      window.URL.revokeObjectURL(link.href);
    },
    getBlob(url) {
      let that = this;
      return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.open('GET', url, true);
        xhr.responseType = 'blob';
        xhr.onload = () => {
          if (xhr.status === 200) {
            resolve(xhr.response);
            that.$message.success(`文件开始下载`)
          } else {
            reject(new Error(`Request failed with status ${xhr.status}`));
          }
        };
        xhr.onerror = () => {
          reject(new Error('Request failed'));
        };

        xhr.send();
      });
    },

# vue  

评论