admin管理员组

文章数量:1323348

FileReader allows to read local file in Chrome.

function readMultipleFiles(evt) {
    var files = evt.target.files;

    if (files) {
        for (var i = 0, f; f = files[i]; i++) {
            var r = new FileReader();
            r.onload = (function (f) {
                return function (e) {
                    var contents = e.target.result;
                    document.getElementById("output").innerHTML = contents;
                };
            })(f);
            r.readAsText(f);
        }
    } else {
        alert("Failed to load files");
    }
}
document.getElementById('fileinput').addEventListener('change', readMultipleFiles, false);​

Run example on jsfiddle

But this example does not work in Internet Explorer 9.
Does IE9 support File API?
If yes - what should I do to have ability to read local files in IE9?

FileReader allows to read local file in Chrome.

function readMultipleFiles(evt) {
    var files = evt.target.files;

    if (files) {
        for (var i = 0, f; f = files[i]; i++) {
            var r = new FileReader();
            r.onload = (function (f) {
                return function (e) {
                    var contents = e.target.result;
                    document.getElementById("output").innerHTML = contents;
                };
            })(f);
            r.readAsText(f);
        }
    } else {
        alert("Failed to load files");
    }
}
document.getElementById('fileinput').addEventListener('change', readMultipleFiles, false);​

Run example on jsfiddle

But this example does not work in Internet Explorer 9.
Does IE9 support File API?
If yes - what should I do to have ability to read local files in IE9?

Share Improve this question edited Sep 11, 2012 at 6:55 Volodymyr Bezuglyy asked Sep 11, 2012 at 6:53 Volodymyr BezuglyyVolodymyr Bezuglyy 16.8k33 gold badges106 silver badges135 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

You can use ActiveX' FileSystemObject.

var fso = new ActiveXObject("Scripting.FileSystemObject");
var a = fso.CreateTextFile("c:\\testfile.txt", true);
a.WriteLine("This is a test.");
a.Close();

If you make an hypertext application (.hta), you will be able to create the ActiveX object FileSystemObject, which will let you access the local file system.

Generally no. Reading local files is a massive security violation.

Does IE9 support File API?

No, it doesn't.

If yes - what should I do to have ability to read local files in IE9?

Did you mean if no? If so then you could use an ActiveX or just inform the user that this feature of your website is not supported on his browser and allow him the possibility to upload the file to the server.

Seems that FileSystemObject like a hidden monster..

var fso, file, fileName = '/file.txt', fileContents = '';
if (fileName) {
    fso = new ActiveXObject('Scripting.FileSystemObject');
    // don't worry about 'camelCase' typing, works both
    if (fso.fileExists(fileName)) {
        file = fso.openTextFile(fileName, 1);
        fileContents = file.readAll();
        // or loop over lines
        // while (!file.atEndOfStream) {
        //     fileContents += file.readLine();
        // }
        file.close();
    }
}

More info: https://msdn.microsoft./en-us/library/314cz14s(v=vs.85).aspx

本文标签: htmlIs it possible in javascript to read local files in IEStack Overflow