1 Ax.sql.Blob
Provides an in memory empty Blob object ready to be filled. When creating the class, you can provide a virtual file name appearing as the name of content.
The Blob is tipically used as a resource for write operations that require and OuputStream
but can also be filled directly using setContent
.
<script> var blob = new Ax.sql.Blob("data.txt"); blob.setContentType("text/plain"); blob.setContent("Hello World"); blob.setContent(` This is a sample text using template string `); console.log("BLOB NAME =" + blob.getFileName()); console.log("TEXT CONTENT=" + blob.getTextContent()); return blob; </script>
BLOB NAME =data.txt
TEXT CONTENT=Hello World
This is a sample text
using template string
void | setContent(byte[] data) | Set contents of blob |
void | setContent(String text) | Set contents of blob using text. UTF-8 encoding will be used for the conversion to bytes |
void | setContent(String text, String charsetName) | Set contents of blob using text using provided charset encoding |
void | setDisposition(String disposition) | Set file disposition |
boolean | isInline() | Is inline getter |
boolean | isAttachment() | Is attachment getter |
byte[] | getContent() | Get byte contents of blob |
String | getTextContent() | Get contents of blob as text. UTF-8 encoding will be used for the conversion from bytes |
String | getTextContent(String charsetName) | Get contents of blob as text using the provided charset encoding |
void | writeTo(JSFile file) | Wtite blob contents to provided file |
1.1 Blobs from database
During SQL operations you can get get or write blob objects. For example, to write and read a blob from database simply do:
<script> var blob = new Ax.sql.Blob("data.txt"); blob.setContentType("text/plain"); blob.setContent(` This is a sample text using template string `); Ax.db.execute("DROP TABLE IF EXISTS test_blob"); Ax.db.execute("CREATE TABLE IF NOT EXISTS test_blob(a integer, b blob)"); Ax.db.insert("test_blob", { "a" : 1, "b" : blob }); var data = Ax.db.executeQuery("SELECT * FROM test_blob").toOne(); console.log(data.b); Ax.db.execute('DROP TABLE test_blob'); </script>
00000000 0A 54 68 69 73 20 69 73 20 61 20 73 61 6D 70 6C .This is a sampl
00000010 65 20 74 65 78 74 0A 75 73 69 6E 67 20 74 65 6D e text.using tem
00000020 70 6C 61 74 65 20 73 74 72 69 6E 67 0A 20 20 20 plate string.
00000030 20
1.2 Write and read strings from blob
Create a blob and write a string to it. The encoding parameter is optional and defaults to UTF-8
<script> var blob = new Ax.sql.Blob("data.txt"); blob.setContentType("text/plain"); blob.setContent(" àèáéòóìí ", "iso-8859-1"); // blob.setContent(text) uses UTF-8 // unreadable characters (using UTF-8) console.log(blob.getTextContent()); // ok console.log(blob.getTextContent("iso-8859-1")); </script>
��������
àèáéòóìí
1.3 Blobs from file
Another class constructor allows to pass a file object (Ax.io.File) and this blob class will read file content and store it in memory.
<script> let file = new Ax.io.File("/tmp/sample.txt"); file.append('123'); var blob = new Ax.sql.Blob(file); return blob; </script>
123
1.4 Blobs from resultset
Another class constructor allows to pass a byte Array and this blob class will create a Blob object with data and store it in memory.
<script> Ax.db.execute('DROP TABLE IF EXISTS test_blob'); Ax.db.execute(`CREATE TABLE test_blob ( file_name varchar(40), file_data blob );`); let file = new Ax.io.File("/tmp/sample.txt"); file.append('123'); var blob = new Ax.sql.Blob(file); Ax.db.execute('INSERT INTO test_blob(file_name, file_data) VALUES ("sample.txt", ?)', blob); var rrs = new Ax.rs.Reader().db(options => { options.setResultSet(Ax.db.executeQuery("SELECT * FROM test_blob")); }).cols().add("MIMETYPE", Ax.sql.Types.CHAR, v => { return new Ax.sql.Blob(v.getBytes("file_data")).getContentType(); }).cols().drop("file_data"); console.log(rrs); Ax.db.execute('DROP TABLE test_blob'); </script>
123
1.5 Publishing blobs
A common problem is how to expose blob content stored in database or filesystem to web site. To solve that, simply select the blob object and get a URL reference.
<script> var blob = new Ax.sql.Blob("data.txt"); blob.setContentType("text/plain"); blob.setContent("Hello World"); blob.setContent(` This is a sample text using template string `); console.log(blob.toMemoryURL()); </script>
blob://localhost/def4a32f-995a-4172-99e3-b7e9dd7661c1