在angularJS中下载本地文件 [英] Download local file in angularJS
问题描述
我有一个可以在Java和AngularJS上运行的应用程序.
I have an application that works on Java and AngularJS.
我用Java创建pdf文件,并使用FileOutputStream
存储它们:
I create pdf files with Java, using FileOutputStream
to store them:
@RequestMapping(value = "/getPdf",
method = RequestMethod.GET)
@RolesAllowed(AuthoritiesConstants.USER)
public List<String> getPdf(@RequestParam(value = "id") Long id){
FileOutputStream fileStream = null;
String fileName = textRepository.findOne(id).getTitle() + ".pdf";
String text = textRepository.findOne(id).getUserText();
try {
fileStream = new FileOutputStream(fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// create an API client instance
Client client = new Client("", "");
client.convertHtml(text, fileName);
try {
fileStream.close();
} catch (IOException e) {
e.printStackTrace();
}
List<String> out = new ArrayList<>();
out.add(fileName);
return out;
}
They are created in the root directory of my application.
现在,我想实现一种功能,该功能使用户可以通过单击链接或按钮来下载 pdf.我已经尝试过$window.open()
,但是我无法设法获取pdf文件的路径.
Now I want to implement a functionality that lets the user to download a pdf by clicking on a link or a button. I have tried with $window.open()
, but I can't manage to get the path to my pdf file.
$scope.getPdf = function (id) {
TextService.getPdf(id).success(function(data){
$window.open('../../../../../../' + data[0], '_blank', 'download');
});
};
Here i get an error saying that Cannot GET /data.pdf
编辑-解决了问题
我必须执行发送文件的 POST 方法:
EDIT - solved the problem
I had to do a POST method that sends the file:
@RequestMapping(value = "/getPdf",
method = RequestMethod.POST)
@RolesAllowed(AuthoritiesConstants.USER)
public ResponseEntity<byte[]> getPdf(@RequestBody Long id){
String filename = textRepository.findOne(id).getTitle() + ".pdf";
String text = textRepository.findOne(id).getUserText();
ByteArrayOutputStream pdf = new ByteArrayOutputStream();
// create an API client instance
Client client = new Client("", "");
client.convertHtml(text, pdf);
byte[] content = pdf.toByteArray();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/pdf"));
headers.setContentDispositionFormData("inline", filename);
headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
ResponseEntity<byte[]> response = new ResponseEntity<>(content, headers, HttpStatus.OK);
return response;
}
回到我的AngularJS客户端,我有一个调用Java方法的服务:
and back to my AngularJS client i have a service that calls the Java method:
angular.module("eddieApp")
.factory("TextService", function($http){
return{
getPdf: function(id){
return $http.post('texts/getPdf', id, { responseType: 'arraybuffer' });
}
};
});
现在在控制器中,我要做的就是调用服务并打开一个带有pdf的窗口:
Now in the controller all i had to do is call the service and open a window with the pdf:
$scope.getPdf = function (id) {
TextService.getPdf(id).success(function(data){
var file = new Blob([data], {type: 'application/pdf'});
var fileURL = ($window.URL || $window.webkitURL).createObjectURL(file);
$window.open(fileURL, '_blank', 'download');
});
};
希望它对某人有帮助!
推荐答案
如果要从Web服务器提供角度部分,则无法访问服务器的文件系统.那将是一个严重的安全问题.
If you are serving the angular portion from a webserver, you cannot access the filesystem of the server. That would be a severe security problem.
为什么不提供另一个使用适当的MIME类型直接将文件提供给用户的@RequestMapping(value = "/getFile")
?
Why not provide another @RequestMapping(value = "/getFile")
which serves the file directly to the user, using the proper MIME type as well?
这是一个类似的问题使用spring MVC返回生成的pdf ,并带有回答如何做.
Here is a similar question Return generated pdf using spring MVC with an answer on how to do that.
这篇关于在angularJS中下载本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!