Today I have been experimenting with SQL binary objects. I started by storing an image in a table, making an AJAX request for a base64 encoding of the image, and displaying it with.
<img src="data:image/jpeg;base64,' + base64imageReturnedWithAjax + '">')
The image displays fine.
The web project I'm working on also requires file downloads (PDF, mainly) - great, I thought, I'll store the PDF as a SQL binary object too, collect it from the server in the same way and then somehow magically decode it for download at the other end.
Help!
I first tried to decode it using a jQuery base64 decoder (https://github.com/carlo/jquery-base64) with:
$.base64.decode(base64fileReturnedWithAjax)
This generates the following error in the console:
Uncaught Cannot decode base64
_getbyte64
_decode
$.ajax.success
f.Callbacks.o
f.Callbacks.p.fireWith
w
f.support.ajax.f.ajaxTransport.send.d
My question is, therefore: Is this a feasible way to handle file downloads? if so, how! and if not, is there an advised way to allow a file to be downloaded from a SQL table?
Kind regards, ATfPT
EDIT: If it helps, this is the webmethod that retrieves and sends the file from the db/server (in VB.NET). Fileblob is the binary object within the database.
'Having connected to the table
While lrd.Read()
Dim fileBytes = CType(lrd("Fileblob"), Byte())
Dim stream = New MemoryStream(fileBytes, 0, fileBytes.Length)
Dim base64String = Convert.ToBase64String(stream.ToArray())
document.Add(base64String)
End While
Dim serializer As New JavaScriptSerializer()
Dim returnVal As String = serializer.Serialize(document)
Return returnVal