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");

About austinfrance

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

Leave a comment