FileReader.readAsArrayBuffer()
FileReader.readAsBinaryString()
FileReader.readAsDataURL()
FileReader.readAsText()
Description
The FileReader object allows apps to read files into memory. Depending on the platform, files can be flat text, photos, movies or other types of files. Certain platforms restrict which files can be opened. Photo Libraries, DropBox, iCloud Drive and Google drive files can usually be opened.
The file to be opened has to be selected by the user from a TextBox control, with inputType set to File. It is not possible to hard code a file name: this is for security purposes. Otherwise, web apps would be able to open files on user's systems that should not be allowed.
The FileReader control can read files, but cannot write them.
To use this function, first create a reader object by calling new FileReader
. Once this is done, the file can be read in using one of the following functions:
.readAsArrayBuffer() |
Starts reading the contents of the specified file, once finished, the result attribute contains an ArrayBuffer representing the file's data.
|
.readAsBinaryString() |
Starts reading the contents of the specified file, once finished, the result attribute contains the raw binary data from the file as a string.
|
.readAsDataURL() |
Starts reading the contents of the specified file, once finished, the result attribute contains a data: URL representing the file's data. Useful for images.
|
.readAsDataURL() |
Starts reading the contents of the specified file, once finished, the result attribute contains the contents of the file as a text string.
|
The call is asynchronous: when the read is complete, the FileReader unload() function is called. An object containing information about what was read in is passed to the function.
Example
var reader;
reader = new FileReader();
txtFilename.onchange = function() {
reader.readAsText(txtFilename.files[0]);
};
reader.onload = function(e) {
var lines = createMultiDimArray('');
txtData.text = "";
txtData.text = txtData.text + "Filename: " + txtFilename.files[0].name + '\n';
txtData.text = txtData.text + "Size: " + txtFilename.files[0].size + '\n';
txtData.text = txtData.text + "Modified: " + txtFilename.files[0].lastModifiedDate + '\n';
lines = e.target.result.split('\n');
txtData.text = txtData.text + "Lines: " + Len(lines) + '\n';
for (i = 0; i <= 50; i++) {
txtData.text = txtData.text + lines[i] + '\n';
}
};
Dim reader
reader = new FileReader();
Function txtFilename_onchange()
reader.readAsText(txtFilename.files[0])
End Function
Function reader_onload(e)
Dim lines()
txtData.text = ""
txtData.text = txtData.text & "Filename: " & txtFilename.files[0].name & vbCRLF
txtData.text = txtData.text & "Size: " & txtFilename.files[0].size & vbCRLF
txtData.text = txtData.text & "Modified: " & txtFilename.files[0].lastModifiedDate & vbCRLF
lines = Split(e.target.result, vbLF)
txtData.text = txtData.text & "Lines: " & Len(lines) & vbCRLF
For i = 0 To 50
txtData.text = txtData.text & lines(i) & vbCRLF
Next
End Function
Output
Related Items