put-image.js loads the content of an file with an image (png) from the local file system and inserts it as BLOB into a new record in the table we have created:
function putImage(filename) {
var blob = conn.createBlob();
var stream = blob.setBinaryStream(0);
var path = java.nio.file.FileSystems.getDefault().getPath(filename);
java.nio.file.Files.copy(path, stream);
stream.flush();
var ret=util.execute(
'insert into lob_example(filename, data) values (:filename , :data)',
{ filename: filename,
data : blob }
);
if (!ret) {
print('Something unintended happened.');
}
}
putImage('1000x650.png');
conn.commit();
With the image now in the database, we can get it again and save it locally:
var images = util.executeReturnList('select filename, data from lob_example',{});
images.forEach(function(image) {
print( image.FILENAME);
var blobStream = image.DATA.getBinaryStream(0);
var path = java.nio.file.FileSystems.getDefault().getPath(image.FILENAME);
java.nio.file.Files.copy(blobStream, path);
});