简体   繁体   中英

How can I use JavaScript to read a local file when the html is local

I would like to be able to read a file that is stored in the same directory as the html file. When the html file is accessed by http, there is no problem; I can just use a HttpXMLRequest. However when the HTML is being read off the local disk (as with File/Open in most desktop browsers, it seems HttpXMLRequest does not work. I used to do this with a Java Applet, but (a) I've noticed that doesn't work with some browsers and (b) I'd like a solution that uses just JavaScript: no Java and no Flash.

HttpXMLRequest as it suggests makes HTTP requests - without a webserver, this isn't possible but it's not hard to setup a localhost server.

Though I'm sure there are other opinionated people who will suggest otherwise; my preference for a windows machine is EasyPHP http://www.easyphp.org/ and XAMP if you're not using the default/preinstalled apache server.

Here is a solution for reading a file into javascript, but I am unclear at present if you are limited to only AJAX style requests. Anyway, if you are not limited to only that method then you could use FileReader .

The following example uses readAsDataURL but you can use one of the other listed methods.

void readAsArrayBuffer(in Blob blob); Requires Gecko 7.0
void readAsBinaryString(in Blob blob);
void readAsDataURL(in Blob file);
void readAsText(in Blob blob, [optional] in DOMString encoding);

So here is the example that will load a file, then convert it to base64 and display the resulting string for you.

CSS

#progress_bar {
    margin: 10px 0;
    padding: 3px;
    border: 1px solid #000;
    font-size: 14px;
    clear: both;
    opacity: 0;
    -moz-transition: opacity 1s linear;
    -o-transition: opacity 1s linear;
    -webkit-transition: opacity 1s linear;
}
#progress_bar.loading {
    opacity: 1.0;
}
#progress_bar .percent {
    background-color: #99ccff;
    height: auto;
    width: 0;
}
#display {
    width: 500px;

height: 150px; }

HTML

<input type="file" id="files" name="file" />
<button id="cancel">Cancel read</button>
<div id="progress_bar">
    <div class="percent">0%</div>
</div>
<div>Base64 encoded result</div>
<textarea id="display"></textarea>

Javascript

/*jslint sub: true, maxerr: 50, indent: 4, browser: true */
/*global */

(function () {
    "use strict";

    var reader,
    progress = document.querySelector(".percent"),
        display = document.getElementById("display"),
        iFiles = document.getElementById("files"),
        bCancel = document.getElementById("cancel"),
        dropZone = document.getElementById("drop_zone"),
        filebox = document.getElementById("filebox"),
        list = document.getElementById("list");

    function abortRead() {
        if (reader) {
            reader.abort();
        }
    }

    function errorHandler(evt) {
        switch (evt.target.error.code) {
            case evt.target.error.NOT_FOUND_ERR:
                alert("File Not Found!");
                break;
            case evt.target.error.NOT_READABLE_ERR:
                alert("File is not readable");
                break;
            case evt.target.error.ABORT_ERR:
                break; // noop
            default:
                alert("An error occurred reading this file.");
        };
    }

    function updateProgress(evt) {
        // evt is an ProgressEvent.
        if (evt.lengthComputable) {
            var percentLoaded = Math.round((evt.loaded / evt.total) * 100);
            // Increase the progress bar length.
            if (percentLoaded < 100) {
                progress.style.width = percentLoaded + "%";
                progress.textContent = percentLoaded + "%";
            }
        }
    }

    function handleFileSelect(evt) {
        // Reset progress indicator on new file selection.
        progress.style.width = "0%";
        progress.textContent = "0%";

        reader = new FileReader();
        reader.onerror = errorHandler;
        reader.onprogress = updateProgress;
        reader.onabort = function (e) {
            alert('File read cancelled');
        };

        reader.onloadstart = function (e) {
            document.getElementById('progress_bar').className = 'loading';
        };

        reader.onload = function (e) {
            // Ensure that the progress bar displays 100% at the end.
            progress.style.width = "100%";
            progress.textContent = "100%";
            setTimeout("document.getElementById('progress_bar').className='';", 2000);
            display.value = e.target.result;
        }

        // Read in the image file as a binary string.
        reader.readAsDataURL(evt.target.files[0]);
    }

    iFiles.addEventListener("change", handleFileSelect, false);
    bCancel.addEventListener("click", abortRead, false);
}());

On jsfiddle

Update: from the W3C File API - working draft

Security Considerations

This specification allows web content to read files from the underlying file system, as well as provides a means for files to be accessed by unique identifiers, and as such is subject to some security considerations. This specification also assumes that the primary user interaction is with the element of HTML forms [HTML], and that all files that are being read by FileReader objects have first been selected by the user. Important security considerations include preventing malicious file selection attacks (selection looping), preventing access to system-sensitive files, and guarding against modifications of files on disk after a selection has taken place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM