gpt4 book ai didi

angularjs - 二进制文件损坏 - 如何使用 AngularJS 下载二进制文件

转载 作者:行者123 更新时间:2023-11-28 03:50:18 29 4
gpt4 key购买 nike

使用 ResponseEntity 和 Angular 下载任何文件不起作用

我需要在客户端使用 Angular 下载文件,该文件可以是任何格式,可以是 pdf、excel、图像或 txt ...我的方法仅适用于 txt 文件,并为我提供了 excel 和图像的失败格式,对于 pdf,它提供了一个空的 pdf。

所以在我的 Controller 中,这是调用服务方法的函数:

    vm.downloadFile = downloadFile;

function downloadFile(file){
var urlDir = "C://STCI//"+idpeticion;
return VerDocServices.downloadFile(file,urlDir)
.then(function(response) {
var data = response.data;
var filename = file;
var contentType = 'application/octet-stream';//octet-stream
var linkElement = document.createElement('a');
try {
var blob = new Blob([ data ], {
type : contentType
});
var url = window.URL.createObjectURL(blob);
linkElement.setAttribute('href', url);
linkElement.setAttribute("download", filename);
var clickEvent = new MouseEvent("click", {
"view" : window,
"bubbles" : true,
"cancelable" : false
});
linkElement.dispatchEvent(clickEvent);
} catch (ex) {
console.log(ex);
throw ex;
}
}).catch(function(response) {
alert('Se ha producido un error al exportar del documento');
console.log(response.status);
throw response;
});
}

我的 service.js 有:

angular.module('mecenzApp').service('VerDocServices',['$http',function($http) {

this.downloadFile = function(file,urlDir) {

return $http.get('api/downloadFile', {
params : {
file : file,
urlDir : urlDir
}
}); }} ]);

我的服务方法是这样的:

@GetMapping("/downloadFile")
@Timed
public ResponseEntity<byte[]> downloadFile(@RequestParam(value = "file") String file, @RequestParam(value = "urlDir") String urlDir) {
log.debug("GET ---------------- DOWNLOAD FILE : {}", file);
log.debug("GET ---------------- From the DIRECTORY: {}",urlDir);

InputStream fileStream;
String filepath = urlDir+File.separator+file;
try {
File f = new File(filepath);
log.debug("GET ---------------- FILE: {}",f.getPath());
fileStream = new FileInputStream(f);
byte[] contents = IOUtils.toByteArray(fileStream);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/octet-stream"));
String filename = file;
headers.setContentDispositionFormData(filename, filename);
ResponseEntity<byte[]> response2 = new ResponseEntity<byte[]>(contents, headers, HttpStatus.OK);
fileStream.close();
return response2;

} catch (FileNotFoundException e) {
System.err.println(e);
} catch (IOException e) {
System.err.println(e);
}
return null;
}

你能看一下并告诉我我错过了什么吗?

谢谢你:)

最佳答案

如何使用 AngularJS 下载二进制文件

下载二进制文件时,设置responseType非常重要:

app.service('VerDocServices',['$http',function($http) {

this.downloadFile = function(url, file, urlDir) {
var config = {
//SET responseType
responseType: 'blob',
params : {
file : file,
urlDir : urlDir
}
};

return $http.get(url, config)
.then(function(response) {
return response.data;
}).catch(function(response) {
console.log("ERROR: ", response.status);
throw response;
});
};
}]);

如果省略 responseType,XHR API 默认转换 UTF-8将文本编码为 DOMString (UTF-16)这会损坏 PDF、图像和其他二进制文件。

有关详细信息,请参阅MDN Web API Reference - XHR ResponseType

关于angularjs - 二进制文件损坏 - 如何使用 AngularJS 下载二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48049773/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com