0

There is a PDF in a BLOB field of a table in the 4Dv15 database that Wakanda is connected to. When I pull the BLOB from the field in a serverside script (in Wakanda), it is an object:

{'size': 12915, 'type': 'application/octet-stream'}

The PDF or binary data do not appear to be in the returned object. I want to deliver the PDF inside the BLOB to the client. Can you help me figure out how to do this?

Thank you

Edit: This is what I get when I try using the code:

console.log(blob); var objectUrl = URL.createObjectURL(blob); window.open(objectUrl);

enter image description here

Edit 2: This is the serverside code I am using to get the blob. I am just trying to get it to work here. There is only one record in the table and it has a PDF in the BLOB.

var reportCollection = ds.ReportLog.all(); var blob = reportCollection[0].ReportBlob;

The result of this code is the object seen above-- I see no evidence that 4D is returning the binary data of the BLOB but instead only the properties of that BLOB.

NAMS
  • 953
  • 6
  • 17

1 Answers1

1

From a Wakanda Request Handler you could use sendChunkedData():

response.sendChunkedData(blob)

Or write the blob to a file on the server using .copyTo() from the BLOB API and serve the file to the client:

var myFile = new File (outputFolder + filename) 
blob.copyTo(myFile)

Another option may be the URL.createObjectURL static method which could be used like this:

var objectUrl = window.URL.createObjectURL(blob)
window.open(objectUrl)

See also: URL.revokeObjectURL

Tim Penner
  • 3,453
  • 19
  • 35
  • Thank you for your reply Tim. I tried using the URL.createObjectURL but got this error (image of error in original post). The line above it is a console log of the BLOB returned from 4D. – NAMS Jun 20 '16 at 18:40
  • @NAMS this may be related to your error: [Failed to execute 'createObjectURL' on 'URL':](http://stackoverflow.com/a/33759534/5971390) - i updated my answer slightly. – Tim Penner Jun 20 '16 at 18:50
  • I looked into that, thank you- I see that the solution in that post was to use the `new Blob()` constructor inside of the `createObjectURL()` function, but one thing that I am missing if I'd try to do this is the actual binary data. When I pull the data in the BLOB field using serverside javascript, it is an object with properties `size` and `type` - never do I actually get binary data from 4D. I am wondering if this is a source of my problem. I get the feeling that the data returned from 4D does not contain the binary data which I need. I edited my post with the SS code to get the blob. – NAMS Jun 20 '16 at 20:21
  • Also, I wanted to mention that in troubleshooting, I deleted a different field from the table: an Object field. Doing this changed the error to `TypeError: Argument 1 is not valid for any of the 2-argument overloads of URL.createObjectURL.` For whatever reason, the presence of Object data in an Object field causes issues, like for example the data browser would not return any records. I am on Wakanda v10 still. It's bizarre that the presence of that other field would cause issues because you can create tables with Object fields in them in v10. Maybe I can't do BLOBs in v10 either? – NAMS Jun 20 '16 at 20:29
  • What about using `.toString()` from the [Blob API](http://download.wakanda.org/Documentation/current/SSJS-BLOB-API-v4.pdf) to check the contents of your blob. – Tim Penner Jun 20 '16 at 22:54
  • When I do `blob.toString()` on the serverside, I get an error object: `{ name: "Error", message: "Cannot convert text from stream using the source encoding", messages: Array(), 2 more}` – NAMS Jun 21 '16 at 14:08
  • Hey Tim, I got it to work on the serverside. When trying to use the BLOB's `copyTo()` function, I was using it incorrectly. This allowed me to write it to a folder on the webserver, which I can then serve to the client. Ideally I wanted to serve the record's BLOB directly to the client but this works. `var myFile = new File (outputFolder + filename) ; blob.copyTo(myFile);` Thank you for all your help. – NAMS Jun 21 '16 at 16:02
  • @NAMS cool - i will update this answer to include that information. – Tim Penner Jun 21 '16 at 16:17