How to read txt from the Open File dialog box and load content into a text box using javascript (jquery)?

I want to create an open file button to import the contents of txt files into a text box.

How can I read the contents of a file without uploading it to the server?

I want to use javascript (with jquery lib) and I want to do this without refreshing the page.

+4
source share
7 answers

I used the HTML 5 file API and it works fine locally.

+1 for Trusktr.

Link: http://www.html5rocks.com/en/tutorials/file/dndfiles/ (file slicing.)

+2
source

HTML5 File API.

, , jQuery , .

+2

javascript , - , , .

- : allMyPasswords.txt:)

0

.

FORM INPUT FILE . .

0

, ajax, , php , .

0

, , . , , :

  • id
  • iframe id
  • iframes target ,
  • , submit. , , , .
  • iframe, , php script - :

    echo "<script>window.top.window.file_ready('$file');</script>";

  • js, file_ready, , ajax .

. AJAX.

0

Vanila JS HTML5. - HTML- textarea :

 <textarea id="textareaid" rows="4" cols="50"></textarea>
 <input type="file" id="files" name="files[]" /><output id="list"></output>

JavaScript, textarea id, 'textareaid' id textarea. uploadAndLoad(); .

function handleFileSelecting(evt) {
    var files = evt.target.files; // FileList object

    // Loop through the FileList and render image files as thumbnails.
    for (var i = 0, f; f = files[i]; i++) {

        var reader = new FileReader();

        // Closure to capture the file information.
        reader.onload = (function(theFile) {
            return function(e) {

                var content = e.target.result;
                if (content) {
                    // Inserting content into textarea, change id if you need
                    var textarea = document.getElementById('textareaid');
                    textarea.innerHTML = content;

                }
            };
        })(f);

        reader.readAsText(f);
    }
}

function uploadAndLoad() {
    var filesElem = document.getElementById('files');
    filesElem.addEventListener('change', handleFileSelecting, false);
    filesElem.click();
}
0

Source: https://habr.com/ru/post/1743750/


All Articles