FileReader: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
No edit summary
Line 1: Line 1:
reader.readAsText()
FileReader.readAsArrayBuffer()
FileReader.readAsBinaryString()
FileReader.readAsDataURL()
FileReader.readAsText()


== Description ==
== Description ==
Line 19: Line 22:
| .readAsDataURL() || Starts reading the contents of the specified Blob, 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 Blob, once finished, the result attribute contains a data: URL representing the file's data. Useful for images.
|-
|-
| .readAsText() || Starts reading the contents of the specified Blob, once finished, the result attribute contains the contents of the file as a text string.  
| . readAsDataURL() || Starts reading the contents of the specified Blob, once finished, the result attribute contains the contents of the file as a text string.  
|}
|}


== Example ==
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 (BASIC) ==
<pre>Dim reader
<pre>Dim reader
reader = new FileReader();
reader = new FileReader();
Line 29: Line 34:
   reader.readAsText(txtFilename.files[0])
   reader.readAsText(txtFilename.files[0])
End Function
End Function
Function reader_onload(e)
Function reader_onload(e)
   Dim lines()
   Dim lines()
Line 49: Line 53:
== Output ==
== Output ==


<pre>
[[File:Readastext.png]]
(nextAction appears 3 seconds later)
</pre>


== Related Items ==
== Related Items ==
[[SetTimeout|ClearInterval]], [[SetTimeout|ClearTimeOut]], [[SetTimeout|SetInterval]]


[[Category:Language Reference]]
[[Category:Language Reference]]


[[Category:Miscellaneous]]
[[Category:Miscellaneous]]

Revision as of 19:53, 16 November 2015

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 Blob, once finished, the result attribute contains an ArrayBuffer representing the file's data.
.readAsBinaryString() Starts reading the contents of the specified Blob, once finished, the result attribute contains the raw binary data from the file as a string.
.readAsDataURL() Starts reading the contents of the specified Blob, 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 Blob, 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 (BASIC)

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