在IE11中下载文件获取错误“'Uint8Array'未定义”

时间:2017-09-20 08:03:45

标签: javascript download blob internet-explorer-11

我正在创建一个我正在下载文件的应用程序。为此,我从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

2 个答案:

答案 0 :(得分:4)

我的角度版本是1.2.26,角色1.5的更高版本支持Uint8Array 并添加

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

答案 1 :(得分:0)

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