标签:angularjs docx spark-java java
我正在使用Spark Java framework创建一个Web应用程序.前端是使用AngularJS开发的.
我想在服务器上(内存中)生成一个.docx文件,并将其发送到客户端进行下载.
为此,我创建了一个角度服务,在用户单击下载按钮后调用了以下功能:
functions.generateWord = function () {
$http.post('/api/v1/surveys/genword', data.currentSurvey).success(function (response) {
var element = angular.element('<a/>');
element.attr({
href: 'data:attachment;charset=utf-8;application/vnd.openxmlformats-officedocument.wordprocessingml.document' + response,
target: '_blank',
download: 'test.docx'
})[0].click();
});
};
在服务器上,此api调用将转发到以下方法:
public Response exportToWord(Response response) {
try {
File file = new File("src/main/resources/template.docx");
FileInputStream inputStream = new FileInputStream(file);
byte byteStream[] = new byte[(int)file.length()];
inputStream.read(byteStream);
response.raw().setContentType("data:attachment;chatset=utf-8;application/vnd.openxmlformats-officedocument.wordprocessingml.document");
response.raw().setContentLength((int) file.length());
response.raw().getOutputStream().write(byteStream);
response.raw().getOutputStream().flush();
response.raw().getOutputStream().close();
return response;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
我试图以多种不同方式解决此问题,但最终我总是得到一个看起来像这样的损坏的“ test.docx”:
解决方法:
通过使用斑点并在$http.post api调用中将响应类型指定为’arraybuffer’来解决该问题.据我所知,此解决方案唯一的缺点是它在IE上无法很好地运行,但这又是一个问题.
functions.generateWord = function () {
$http.post('/api/v1/surveys/genword', data.currentSurvey, {responseType: 'arraybuffer'})
.success(function (response) {
var blob = new Blob([response], {type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'});
var url = (window.URL || window.webkitURL).createObjectURL(blob);
var element = angular.element('<a/>');
element.attr({
href: url,
target: '_blank',
download: 'survey.docx'
})[0].click();
});
};
我认为问题出在当我尝试使用以下方法创建URL时,字节流被编码为纯文本:
href: 'data:attachment;charset=utf-8;application/vnd.openxmlformats-officedocument.wordprocessingml.document' + response
从而破坏了它.
当使用blob代替时,我获得了到生成的字节流的“直接”链接,并且由于响应类型设置为“ arraybuffer”,因此未对其进行编码.
请注意,这只是我自己对原始代码出问题的原因进行推理.我可能错得很厉害,如果是这样,请随时纠正我.
标签:angularjs,docx,spark-java,java 来源: https://codeday.me/bug/20191120/2044571.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。