RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
下载文件流文件名转码-创新互联

下载文件,后端返回文件流,前端下载并读取后端返回文件名content-disposition

成都创新互联专业IDC数据服务器托管提供商,专业提供成都服务器托管,服务器租用,西部信息机房西部信息机房,成都多线服务器托管等服务器托管服务。

后端接口返回content-disposition:

前端读取文件名并下载

1.axios返回请求响应全部数据

service.interceptors.response.use((response: AxiosResponse) =>{
  const result = response.data;
  const { config: { responseType } } = response;
  if (responseType === 'blob') {
    return response;
  }

2.接口调用返回中读取解码:decodeURI(fileName)

getExport().then((res:any) =>{
          const fileName = res.headers['content-disposition'].split(';')[1].split('filename=')[1];
// 转换编译content-disposition的文件名:decodeURI
          downloadFile(decodeURI(fileName), res.data);
        });

3.文档流下载方法

export function downloadFile(fileName:string, content:Blob, unicode = 'application/octet-stream;charset=utf-8'):void {
  // const blob = new Blob([content], { type: unicode });
  if ('download' in document.createElement('a')) { // 非IE下载
    const downloadElement = document.createElement('a');
    let href = '';
    if (window.URL) {
      href = window.URL.createObjectURL(content);
    } else {
      href = window.webkitURL.createObjectURL(content);
    }
    downloadElement.href = href;
    downloadElement.download = fileName;
    document.body.appendChild(downloadElement);
    downloadElement.click();
    if (window.URL) {
      window.URL.revokeObjectURL(href);
    } else {
      window.webkitURL.revokeObjectURL(href);
    }
    document.body.removeChild(downloadElement);
  }

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


网站标题:下载文件流文件名转码-创新互联
标题链接:http://cqwzjz.cn/article/geijd.html