Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML 4 equivalent of HTML 5's FileReader?

I've got a web page which needs to be able to load files into the DOM from the local machine on which the browser is running. I've found that this is very easy to do using the HTML 5 File API.

I can just do:

var reader = new FileReader();  
reader.onload = function (fileContents) { ... load contents to a div ... }
reader.readAsText(f) //where f is an HTML5 File object

Annoyingly, I need this to work in IE7, and also some earlier versions of Firefox which don't support the API. Is there any easy way to load a local file into the DOM in older browsers?

Many thanks!

like image 646
Jon Artus Avatar asked Mar 22 '11 21:03

Jon Artus


Video Answer


2 Answers

No, you cannot do that in older browsers. FileReader (any file system access really) is a new HTML5 feature which is not supported in older browsers.

Your best option in an older browser is either:

  1. A Silverlight, Flash or Java app (or similar) that runs on the client-side and has local file system access.
  2. Having the user upload files using the <input type="file"> element, and do your processing server-side.
like image 162
driis Avatar answered Oct 05 '22 20:10

driis


Further to the other answers here, it does appear that there's no consistent way of doing this client-side (other than Flash) for older browsers.

For IE7/8 however, I've managed to hack something together using ActiveX.

var filePath = f:\oo.txt;
var fso = new ActiveXObject("Scripting.FileSystemObject");
var textStream = fso.OpenTextFile(filePath);
var fileData = file.ReadAll();

I can then pass this to the same function as reader.onload in the question above. Obviously this is a bad, hacky solution, and liable to be blocked by some security policies - it does at least work for IE7 though!

like image 44
Jon Artus Avatar answered Oct 05 '22 20:10

Jon Artus