前端如何获取响应头Content-Disposition中的filename,获取文件类型fileType进行不同类型的文件预览

文章介绍了如何使用axios从服务器获取文件,通过content-disposition头判断文件类型,然后针对不同类型的文件(如PDF、图片、Word文档等)进行处理,包括创建URL对象、转换格式和下载。
摘要由CSDN通过智能技术生成

  // 预览文件
        preview: function(item) {
          var vm = this;
          if (!item.raw) {
            var itemBasicId = this.form.itemBasicId

            var url =ctx + '/aea/item/basic/getItemDocOrTemplateFile.do?itemBasicId='+itemBasicId+'&fileType=applyTableTemplate'
            
            axios({
            method: 'GET',
            headers: {  'Authorization': 'bearer '+localStorage.getItem("access_token") },
            url: url,
            originResponse: true,
            responseType: 'blob' // 更改responseType类型为 blob
          }).then( function(res) {
              vm.previewPdfAndPic(res.data)
          }).catch(function(err) {
            console.log(err)
          })
          }else{
          //  vm.openBlobFile(item)
          }
        },
       previewPdfAndPic: function (response) {
  
        var _fileName = '申请表模板.docx', fileType = 'pdf';

        if(response.headers['content-disposition']){
          var temp = response.headers['content-disposition'];
          _fileName = temp.split(';')[1].split('filename=')[1]; 

          fileType = _fileName.substring(_fileName.lastIndexOf('.')); 
          var fileName = _fileName.substring(0, _fileName.lastIndexOf('.')); // 文件名
        }
        console.log(fileName)
        console.log(fileType)
        try {

          if(fileType.toLocaleLowerCase === 'pdf'){
          // 转换pdf
          var blob = new Blob([response], { type: 'application/pdf' });
          var url = window.URL.createObjectURL(blob);
          var tempwindow=window.open(); // 先打开页面
          tempwindow.location= url
        }else if(['.png', '.jpg','.jpeg', '.gif'].indexOf(fileType.toLocaleLowerCase) !== -1){
            var imgBlob = new window.Blob([response], { type: 'image/png' })
            var url = window.URL.createObjectURL(blob);
            var tempwindow=window.open(); // 先打开页面
            tempwindow.location= url
        }else if(['.doc', '.docx'].indexOf(fileType.toLocaleLowerCase) !== -1){
          var downloadUrl = window.URL.createObjectURL(new Blob([response], { type: "application/octet-stream" }));
          var link = document.createElement('a');
          link.style.display = 'none';
          link.href = downloadUrl;
          link.download = decodeURI(_fileName);
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        }
          
        } catch (error) {

          var downloadUrl = window.URL.createObjectURL(new Blob([response], { type: "application/octet-stream" }));
          var link = document.createElement('a');
          link.style.display = 'none';
          link.href = downloadUrl;
          link.download = decodeURI(_fileName);
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        }
        
        
       }, 
       openBlobFile: function (item) {
          if(item.raw) {
            const tempFileType = item.name.substring(item.name.length - 4)
              if (['.pdf', '.PDF'].indexOf(tempFileType) !== -1) {
                state.ImgSrc = null
                state.PDFURL = window.URL.createObjectURL(props.fileRaw)
                openTips.close()
                return
              }
              if (['.png', '.jpg','.jpeg', '.gif'].indexOf(tempFileType) !== -1) {
                state.PDFURL = null
                const reader = new FileReader();
                reader.onload = (evt) => {
                  state.ImgSrc = evt.target.result
                };
                reader.readAsDataURL(props.fileRaw);
              }
          }
        },
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

春风得意之时

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值