close

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.Open();
  inputStream.Type = 1;  // adTypeBinary
  inputStream.LoadFromFile(filePath);
  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, '');
}
 
But it cannot work in IE9. I get an error saying Access is denied, IE doesn't allow manipulation of the type="file" input element from javascript due to security reasons. Setting the filename or invoking a click event to show the browser dialog will result in an "Access is denied" error on the form submit - Internet Explorer is clever about remembering what methods have been invoked.
 
arrow
arrow
    文章標籤
    javascript base64 Image file
    全站熱搜

    Kenneth 發表在 痞客邦 留言(0) 人氣()