How to create a blob on the server side?

709 views
Skip to first unread message

Buddha Shrestha

unread,
Dec 14, 2016, 9:00:31 PM12/14/16
to vert.x

Hi,

In my server (back end), I'm creating a pdf/xls generating tool using jasper reports. 

In my client side (front end), I'm using angularJs and consuming those reports in-order to download them from browser.

and set the headers as mentioned.

But, on my client side, while trying to convert the output from the server response into a BLOB, all I get is [object object].

I haven't used angularJs that much, and I'm finding it hard to debug this. 

So, can we verify from server end that the response, we have created is a valid blob?

Paulo Lopes

unread,
Dec 15, 2016, 5:20:34 AM12/15/16
to vert.x
What is a blob? blob is a type from databases that stands for binary long object. You're producing a pdf in the server side, so your jasper reports will return a byte array or something similar, in order to return it properly to your client (angular app) you need to specify the content type "application/pdf" and write the bytes to the response. Probably you're missing the content type header so your browser and angular will not recognize the response as a pdf and will assume it to be text of something else...

Buddha Shrestha

unread,
Dec 15, 2016, 5:35:00 AM12/15/16
to vert.x
I guess I have specified all the things required. Here is my sample response:

ByteArrayOutputStream outputStream = (ByteArrayOutputStream) resultSet.getValue();
byte[] bytes = outputStream.toByteArray();
Buffer buffer = Buffer.buffer(bytes);
routingContext.response().setChunked(true);
routingContext.response().putHeader("Content-Type", "text/xls");
routingContext.response().write(buffer);
routingContext.response().setStatusCode(200).end();

This works for csv, but not for xls.

Paulo Lopes

unread,
Dec 15, 2016, 5:51:42 AM12/15/16
to vert.x
I don't think you can easily use chunked responses in angular you would need a http interceptor i think (but I'm no expert here) However looking at your code you only do 1 chunk so you can avoid it and it will return the data correctly to your app:

ByteArrayOutputStream outputStream = (ByteArrayOutputStream) resultSet.getValue();
byte[] bytes = outputStream.toByteArray();
Buffer buffer = Buffer.buffer(bytes);

routingContext.response()

  .putHeader(
"Content-Type", "application/vnd.ms-excel") // i think this is the official mime type for excel...
  .end(buffer);

Reply all
Reply to author
Forward
0 new messages