• js处理后端返回的不同文件的流


      var fileformData = new FormData();
                fileformData.append('file1',vm.fileListLeft[0].raw);
                fileformData.append('file2',vm.fileListRight[0].raw);
                axios.post(ctx + 'fire/drawing/compare',fileformData,{responseType: 'blob'}).then(function (res) {
                    try{
                        parent.vm.compareLoading = false;
                    }catch (e){
                        vm.compareLoading = false;
                    }
                    if (res.status === 200 && res.data) {
                        var disposition = res.headers['content-disposition'];
                        var contentType = res.headers['content-type'];
                        if(contentType.includes('json')){
                            var data = res.data;
                            var reader = new FileReader();
                            reader.onload = function () {
                                var errorInfos = JSON.parse(reader.result);
                                var msg = errorInfos.content;
                                try{
                                    parent.vm.comparePdfShow = false;
                                    parent.vm.compareLoading = false;
                                }catch (e){
                                    vm.comparePdfShow = false;
                                    vm.compareLoading = false;
                                }
                                vm.$message.error(msg||'操作失败,请稍后重试!');
                            }
                            reader.onerror = function (err) {
                                vm.$message.error('转换失败,请稍后重试!');
                            }
                            reader.readAsText(data);
                        }else{
                            try {
                                var fileName = decodeURI(disposition.substring(disposition.indexOf('filename=') + 9, disposition.length));
                                vm.fileType = vm.getFileType(fileName);
                                var blob = res.data;
                                var objectURL = window.URL.createObjectURL(blob);
                                if(vm.fileType == "pdf"){
                                    try{
                                        parent.vm.fileType = "pdf";
                                        parent.vm.pdfUrl = ctx + 'fire/drawing/viewPdf.html?fileUrl='+objectURL;
                                    }catch (e){
                                        vm.fileType = "pdf";
                                        vm.pdfUrl = ctx + 'fire/drawing/viewPdf.html?fileUrl='+objectURL;
                                    }
                                }else{
                                    try{
                                        parent.vm.imgUrl = ctx + 'fire/drawing/viewPic.html?objectURL='+objectURL+'&fileName='+fileName;
                                    }catch (e){
                                        vm.imgUrl = ctx + 'fire/drawing/viewPic.html?objectURL='+objectURL+'&fileName='+fileName;
                                    }
                                }
                            } catch (e) {
                                try{
                                    parent.vm.comparePdfShow = false;
                                    parent.vm.compareLoading = false;
                                }catch (e){
                                    vm.comparePdfShow = false;
                                    vm.compareLoading = false;
                                }
                                vm.$message.error(res.data.content||'操作失败,请稍后重试!');
                            }
                        }
                    } else {// 其它情况
                        try{
                            parent.vm.comparePdfShow = false;
                            parent.vm.compareLoading = false;
                        }catch (e){}
                        vm.$message.error(res.data.content||'操作失败,请稍后重试!');
                    }
                }).catch(function (error) {
                    try{
                        parent.vm.comparePdfShow = false;
                        parent.vm.compareLoading = false;
                    }catch (e){}
                    vm.$message.error('对比出错,请稍后重试!');
                });

    作者:freddyhuang
    出处:https://www.cnblogs.com/freddyhuang
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    .NET core webApi 使用JWT验证签名
    sudo
    Mysql Error Code : 1436 Thread stack overrun
    Parallel World 4 – Parallel Task (1)
    SQLSTATE[HY000] [2002] Can't connect to local MySQL server
    Parallel World 3 – Parallel Task (2)
    Parallel World 5 – Concurrent Collections (1)
    Utime failed: Permission denied in Smarty/sysplugins/smarty_internal_template.php on line xxx
    Add Reference
    Javascript Tips
  • 原文地址:https://www.cnblogs.com/freddyhuang/p/15658588.html
Copyright © 2020-2023  润新知