How to convert SQL Blob to Text in JavaScript

The problem I was facing was that I had some text in a database stored in an image field (blob data), and I wanted to get at that text in javascript.  After a lot of searching and some experimentation, I came up with the following code:-

var blob2Text = function(blobField, charset) {
        var stream = new ActiveXObject("ADODB.Stream");
        stream.Charset = charset;
        stream.Type = 1;  // binary
        stream.Open();
        stream.Write(blobField);
        stream.Position = 0;
        stream.Type = 2;  // text
        var text = stream.ReadText(-1);
        stream.Close();
        return text;
};

Use as follows:-

var rs = new ActiveXObject("ADODB.Recordset");
... code to query data ...
var text = blob2Text(rs("myBlobData"), "us-ascii");
Advertisements

About austinfrance

Technical Developer @ RedSky IT / Explorer Software
This entry was posted in JavaScript, WScript and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s