开发者

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 ... }
read开发者_StackOverflow社区er.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!


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.


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!


Looks like you can do that through Flash. Flash alternative for FileReader HTML 5 API

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜