【问题标题】:Download file in IE11 get error "'Uint8Array' is undefined"在 IE11 中下载文件得到错误“'Uint8Array' 未定义”
【发布时间】:2018-03-01 04:32:37
【问题描述】:

我正在创建一个下载文件的应用程序。为此,我从 js 中的 java 类获得响应并下载此响应。为此,我的 java 代码是 -

@ApiOperation(value = "",
            notes = "")
@Path("/getProjectJSONTODRAW/{implementation}")
@GET
@Timed
public Response getProjectJSONTODRAW(@PathParam("implementation") String implementation) {
        File file = new File(path+File.separator+fileName);
        InputStream inputStream =null;
        String mimeType =null;
        if (!file.exists()) {
            String errorMessage = "Sorry. The file you are looking for does not exist";
            log.info(errorMessage);
        }else {
            mimeType = URLConnection.guessContentTypeFromName(file.getName());
            if (mimeType == null) {
                log.info("mimetype is not detectable, will take default for the file "+file.getName());
                mimeType = "application/octet-stream";
            }
            try {
                inputStream = new BufferedInputStream(new FileInputStream(file));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        return Response
                .ok(inputStream, mimeType)
                .header("Content-Disposition", "attachment; filename=\""+file.getName()+"\"")
                .header("Content-Length", file.length())
                .build();
}

在 JS 代码中是 -

     $http.get('/api/1/explore/getProjectJSONTODRAW/implementation', {
                         responseType: 'arraybuffer'
                     })
                     .success(function(response) {
                        var a = document.createElement("a");
                        document.body.appendChild(a);
                        var fileName = "abc.pdf";
                        var mimeType = "application/pdf";
                        var blob = new Blob([response], {
                           type: mimeType
                        }),
                        url = window.URL.createObjectURL(blob);
                        a.href = url;
                        a.download = fileName;
                        var isIE = false || !!document.documentMode;
                        if (isIE) {
                           a.style.cssText = "display: none;"
                           window.navigator.msSaveOrOpenBlob(blob, fileName);
                        } else {
                           a.style = "display: none";
                           a.click();
                           window.URL.revokeObjectURL(url);
                        }
                   }).catch(function(error) {
                        console.log(error);
                   });
}

这给了我错误

var blob = new Blob([response], {type: mimeType})

错误是 - “'Uint8Array' 未定义” 而我的 IE 版本是 - IE11

【问题讨论】:

标签: javascript download blob internet-explorer-11


【解决方案1】:

我的 Angular 版本是 1.2.26,Uint8Array 在 Angular 1.5 的更高版本中受支持 并添加

<meta http-equiv="X-UA-Compatible" content="IE=11" />

【讨论】:

    【解决方案2】:

    如果Blob 没有抛出错误,您可以使用responseType: "blob"

    【讨论】:

      猜你喜欢
      • 2017-05-02
      • 1970-01-01
      • 2011-08-05
      • 2017-07-20
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      • 1970-01-01
      • 2018-07-23
      相关资源
      最近更新 更多