Hi,
Using XS, I am trying to write a web service that allows for a binary file to be uploaded to my repository. After a quite a lot of trial and error, I finally figured out that using a mulitpart POST request, I could use the following code to receive the file contents on the server side:
function getFile() { var file = {}; try { var contents = ""; var i; for (i = 0; i < $.request.entities.length; i++) { if ($.request.entities[i].body) { contents += $.request.entities[i].body.asString(); } } file.type = $.request.entities[0].contentType; file.contents = contents; } catch (e) { throw "Failed to upload file: EXCEPTION=" + e.message; } return file; }
Using the debugger, I can see that file.contents seems to properly contain the binary contents of the uploaded file.
However, I have been unable to get the contents transferred to the HANA repository. Here is the code which updates an BLOB field with the uploaded content:
function updateBlobField(id, blobFile) { var conn = null; var preparedStatement = null; try { conn = $.db.getConnection(); var sql = 'update "MYTABLE" set "BLOBFIELD"=? where "ID"=?'; preparedStatement = conn.prepareStatement(sql); preparedStatement.setBlob(1, blobFile.contents); preparedStatement.setInteger(2, id); var count = preparedStatement.executeUpdate(); if (count !== 1) { throw "Failed to update blob field"; } conn.commit(); } catch (e) { throw "Failed to update blob field: EXCEPTION=" + e.message; } finally { if (preparedStatement) { preparedStatement.close(); } if (conn) { conn.close(); } } }
When I execute the code, I get the following exception:
PreparedStatement
.setBlob: expected blob for second argument, but got: string
I have tried to use setClob instead of setBlob. Doing so, the code executes,but as I expected, incorrect data is written to the repository.
According to the setBlob documentation (see http://help.sap.com/hana/SAP_HANA_XS_JavaScript_Reference_en/$.db.PreparedStatement.html) it should be able to accept a string parameter as well as an ArrayBuffer. But this is clearly not working as documented.
Since, I could not use a string, I thought I would convert the string into a ArrayBuffer. But I could not figure out how to even create an ArrayBuffer object (HANA Studio complained that ArrayBuffer was used before it was declared and I found little documentation), let alone do the conversion.
Any help would be greatly appreciated.
Thanks,
David