Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Enable JavaScript file API in IE8

I have developed a web application in asp.net, there is a page in this project which user should choose a file in picture format (jpeg, jpg, bmp,...) and I want to preview image in the page but I don't want to post file to server I want to handle it in client i have done it with java scripts functions via file API but it only works in IE9 but most of the costumers use IE8 the reason is that IE8 doesn't support file API is there any way to make IE8 upgrade or some patches in code behind I mean that check if the browser is IE and not support file API call a function which upgrades IE8 to IE9 automatically.

I don't want to ask user to do it in message I want to do it programmatically!!
even if it is possible to install a special patch that is required for file API because customers thought it is a bug in my application and their computer knowledge is low what am I supposed to do with this?
I also use Async File Upload Ajax Control But it post the file to server any way with ajax solution and HTTP handler but java scripts do it all in client browser!!!

following script checks the browser supports API or not

<script>
if (window.File && window.FileReader && window.FileList && window.Blob) 
  document.write("<b>File API supported.</b>");
else
  document.write('<i>File API not supported by this browser.</i>');
</script>   

following scripts do the read and Load Image

function readfile(e1)
{
  var filename = e1.target.files[0]; 
  var fr = new FileReader();
   fr.onload = readerHandler;  
  fr.readAsText(filename); 
} 

HTML code:

<input type="file" id="getimage">

<fieldset><legend>Your image here</legend>
    <div  id="imgstore"></div>
</fieldset> 

JavaScript code:

<script>
function imageHandler(e2) 
{ 
  var store = document.getElementById('imgstore');
  store.innerHTML='<img src="' + e2.target.result +'">';
}

function loadimage(e1)
{
  var filename = e1.target.files[0]; 
  var fr = new FileReader();
  fr.onload = imageHandler;  
  fr.readAsDataURL(filename); 
}

window.onload=function()
{
  var x = document.getElementById("filebrowsed");
  x.addEventListener('change', readfile, false);
  var y = document.getElementById("getimage");
  y.addEventListener('change', loadimage, false);
}
</script>
like image 460
saeed Avatar asked Aug 27 '12 10:08

saeed


People also ask

How do I enable JavaScript in IE 11?

Internet ExplorerClick Tools > Internet Options. Click the Security tab > Custom Level. In the Scripting section, click Enable for Active Scripting. In the dialog box that displays, click Yes.

Does IE 8 support JavaScript?

According to Wikipedia, IE8 only supports Javascript 1.5. So they are saying IE8 completely ignores Javascript versions 1.6, 1.7, 1.8 and 1.9.


2 Answers

You can't install anything special to add support for File API in IE8. What you can do is use a polyfill in order to implement this functionality in browsers that don't natively support it. Take a look at this list of HTML5 Cross Browser Polyfills (you should find something suitable for you in File API / Drag and Drop section).

like image 112
tpeczek Avatar answered Sep 20 '22 06:09

tpeczek


This works for me in pre IE10, i use https://github.com/eligrey/FileSaver.js for every other browser, note though this works it's not perfect, because it's IE and well you know what I mean

Hope this helps

/**
 * saves File, pops up a built in javascript file as a download
 * @param {String} filename, eg doc.csv
 * @param {String} filecontent eg "this","is","csv"
 * @param {String} mimetype eg "text/plain"
 */
function saveAs (filename, filecontent, mimetype) {
    var w = window.open();
    var doc = w.document;
    doc.open( mimetype,'replace');
    doc.charset = "utf-8";
    doc.write(filecontent);
    doc.close();
    doc.execCommand("SaveAs", null, filename);
}
like image 37
aqm Avatar answered Sep 22 '22 06:09

aqm