FileReader.result

FileReader.result

This property returns the contents of the file. This property is only valid after the read operation is completed, and the format of the data depends on the method for starting the read operation. FileReader]**result**

syntax

var file = instanceOfFileReader .result

value

Or the appropriate string ArrayBuffer] which to start reading method based on a read operation. This value is nullwhether or not the reading has not been completed successfully.

The results of the following type.

method description
readAsArrayBuffer() Of resultthe JavaScript ArrayBufferbinary data it contains.
readAsBinaryString() The resultcontained in the original data file is a binary string.
readAsDataURL() This resultis a string data:URL transmitting data representation files.
readAsText() This resultis a text string.

Case

This example shows a function, read()the function reads the file from the file input. The way it works is to create an FileReaderobject and creates a listener for the load event so that when reading the file resultacquired and passed to the callback function to provide read().

As the contents of the original text data processing.

var fileInput = document.querySelector('input[type="file"]');

function read(callback) {
  var file = fileInput.files.item(0);
  var reader = new FileReader();

  reader.onload = function() {
    callback(reader.result);
  }

  reader.readAsText(file);
}

Guess you like

Origin www.cnblogs.com/TMesh/p/11832808.html