JScript – Convert Image To Base64

Yesterday I was updating an HTA (HTML Application) and needed to figure out how to convert an image into a base64 code that could be used in a stylesheet. I needed this to be able to embed an image in the source code instead of referencing an actual image. For this reason I wrote the following function (with the help of this post):

function convertImageToBase64(filePath) {
  var inputStream = new ActiveXObject('ADODB.Stream');
  inputStream.Type = 1;  // adTypeBinary
  var bytes = inputStream.Read();
  var dom = new ActiveXObject('Microsoft.XMLDOM');
  var elem = dom.createElement('tmp');
  elem.dataType = 'bin.base64';
  elem.nodeTypedValue = bytes;
  var ret = 'data:image/png;base64,' + elem.text.replace(/[^A-Z\d+=\/]/gi, '');

As you can see the convertImageToBase64 function takes the file path to the image and returns the base64 encoding as a PNG (regardless of the starting image format). You can change the format to another type, but I just stuck with PNG since it is a nice format to work with. 😎

Batch Excel To CSV Converter Application

I have been truly fascinated by the amount of visitors that I get because of the simple XLS/XLSX to CSV converter script that I made a while back (original post). For that reason I have created an HTML application (HTA) which offers the same functionality and then some:
XLS To CSV Converter

How this Differs from the Script

  • There is an easy to use interface.
  • You can customize how the resulting files are named.
  • You can filter which sheets will be exported as CSVs using simple text matches and/or regular expressions.
  • You will see the results of the conversion process in one place after the process is finished.

Using It

  • Download it from here.
  • Unzip the .HTA file.
  • Double-click on the .HTA file.
  • Click the Browse for Folder button and browse for the folder that contains the XLS and/or XLSX files whose worksheets should be exported as CSVs.
  • Modify the CSV Naming Schema as you like (hints are given in the application).
  • If you would like to only export worksheets with specific names, enter those names in the Sheet Filters textbox (only one per line). You may also use JavaScript-style regular expressions (one on each line) to match the sheets that you want to convert.
  • If you would like to remove the linefeed characters from all cells, check the appropriate checkbox.
  • Click the Start Conversion button and wait for the results to appear.

Version History

  • 1.0 – The first version.
  • 1.0.1
    • Fixed the error thrown when specifying a normal string as a “Sheet Filter”.
    • Fixed the copyright symbol in the “About” message box.
  • 1.1.0
    • Added the ability to specify the delimiter.

Plans for Improvement

As of right now this application is very simple, but I do hope to beef it up a little as time goes on. Let me know if you have any comments, questions, suggestions, or issues with this new application. Most of all, have fun!!! 😎