Attempting to download a file using javascript's Blob object has resulted in some unexpected behavior. Initially, I am utilizing JAX-RS to send the file from the backend. Below is the code snippet:
Workbook wb = new HSSFWorkbook();
//creating workbook...
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
wb.write(bos);
bos.close();
} catch(Exception e) {
e.printStackTrace();
}
byte[] bytes = bos.toByteArray();
return Response.ok(bytes).header("Content-Disposition", "attachment; filename=" + "test.xls").build();
The 'bos' variable represents a 'ByteArrayOutputStream' object created from the Apache POI workbook. When downloading the file using the regular method by pointing the browser to the JAX-RS resource, everything works fine. However, when attempting to implement the solution provided at this link, the downloaded file is corrupted.
Upon debugging in Chrome's console, an observation was made regarding the difference in size between the response and Blob objects while converting the response to Blob format. The response size shows as 4096 bytes:
Date: Tue, 10 Feb 2015 17:32:27 GMT
Server: WildFly/8
Connection: keep-alive
X-Powered-By: Undertow/1
Content-Length: 4096
Content-Disposition: attachment; filename=test.xls
Content-Type: application/vnd.ms-excel
However, upon performing the conversion with
var blob = new Blob([response], { type: type });
, the Blob object size increases to 7836 bytes. This discrepancy in sizes raises questions about why this issue occurs and what causes it.
The problematic behavior was consistent not only in Chrome Version 40.0.2214.111 (64-bit) but also in Firefox 35.0.1.
Thank you for any insights or assistance in addressing this matter.
P.S. The complete client-side code related to the issue mentioned above can be found on this Stack Overflow question page: SO Question.