I have some error of open file.xlsx, genereted by librart Apache POI. File is save fine and open fine on my local machine. But after sending stream from java to vue it doesn't work. This is java:
public void sendFileToResponse(HttpServletResponse response, SearchParam searchParam) throws `IOException {`
String type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
String fileNameAttr = "filename=MyExcel.xlsx";
response.setHeader("Content-Disposition", "attachment;" + fileNameAttr);
response.setHeader("Access-Control-Allow-Origin", "*");
response.setCharacterEncoding("utf-8");
response.setContentType(type + ";charset=UTF-8");
Workbook workbook;
FileOutputStream fileOutputStream = null;
ServletOutputStream outputStream = null;
try {
workbook = getFile(searchParam);
fileOutputStream = new FileOutputStream("MyExcel2.xlsx");
workbook.write(fileOutputStream);
outputStream = response.getOutputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
workbook.write(baos);
byte[] xlsx = baos.toByteArray();
outputStream.write(xlsx);
workbook.close();
fileOutputStream.flush();
fileOutputStream.close();
} catch (Exception e) {
logger.error("Error: {}", e);
}
here i get it on frontend:
saveStatistic () {
client.get('/statistics/statisticUseOfData/download', {responseType: 'blob'})
.then(response => {
console.log(response)
this.downloadFileName = 'test.xlsx'
var aHref = document.createElement('a')
var binaryData = [];
binaryData.push(response.data);
var urlBlob = window.URL.createObjectURL(new Blob(binaryData, {type: "application/octet-stream"}))
aHref.href = urlBlob
aHref.download = this.downloadFileName
document.body.appendChild(aHref)
aHref.click()
window.URL.revokeObjectURL(urlBlob)
})
.catch(error => {
console.log(error)
})
and that part of response, which i have in console.log(response.data):
PKw1Q[Content_Types].xml�S�n�0����*6�PU�C���\{�X�%����]8�R�
q�cfgfW�d�q�ZCB|��|�*�*h㻆},^�{Va�^K<4�6�N�XQ�dž�9�!P��$��҆�d�c�D�j);��ѝP�g��E�M'O�ʕ����H7L�h���R���G��^�'�{��zސʮB��3�˙��h.�h�W�жF�j娄CQՠ똈���}ιL�U:D�����%އ����,�B���[� �� ;˱� �{N��~��X�p�ykOL��kN�V��ܿBZ~����q�� �ar��{O�PKz��q;PKw1Q_rels/.rels���j�0�_���8�`�Q��2�m��4[ILb��ږ���.[K
�($}��v?�I�Q.���uӂ�h���x>=��@��p�H"�~�}� �n����*"�H������8�Z�^'�#��7m{��O�3���G�u�ܓ�'��y|a�����D� ��l_EYȾ����vql3�ML�eh���*���\3�Y0���oJ� :��^��}PK��z��IPKw1QdocProps/app.xmlM��