ReadFile: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
mNo edit summary
No edit summary
 
(11 intermediate revisions by 3 users not shown)
Line 1: Line 1:
ReadFile (''filename''[,''method''])
ReadFile (''filename''[,''method''[, callBack]])


== Description ==
== Description ==
Line 5: Line 5:
Note: This function can be replaced by the [[Ajax]]() function.
Note: This function can be replaced by the [[Ajax]]() function.


ReadFile will read ''filename'' which can either be deployed with the app or be on the same server as the app. If the file is to be deployed with the app, include it in the manifest. This function will only work when deployed to a website: it will not work when running in the local browser.  
ReadFile will read ''filename'' which can either be deployed with the app or be on the same server as the app. If the file is to be deployed with the app, include it in the manifest. This function will only work when deployed to a website: it will not work when deployed to a local folder.


Use ''method'' to define the access method. Usually this parameter can be left out: the default is Get. The next most common value for this is Post. Post responses are never cached, whereas Get responses can be. Post also allows larger data transfers. Other methods are Connect, Delete, Head, Options, Put, Trace, or Track. For more information on these options, look up XMLHttpRequest on the web. The function is based on XMLHttpRequest. See the ReadFile.nsx sample.
Use ''method'' to define the access method. Usually this parameter can be left out: the default is Get. The next most common value for this is Post. Post responses are never cached, whereas Get responses can be. Post also allows larger data transfers. Other methods are Connect, Delete, Head, Options, Put, Trace, or Track. For more information on these options, look up XMLHttpRequest on the web. The function is based on XMLHttpRequest. See the ReadFile.nsx sample in Samples folder 7 "Ajax and ReadFile".
 
If you supply a ''callBack'' argument, a function with that name will be called when the read is complete. Adding this option will cause the operation to become asynchronous. The next statement after ReadFile will be executed immediately, before the read is complete. This results in smoother operation for the user, especially if the file is on a remote server.
 
If you do not use a ''callBack'', you will see a warning in the [[Using the Chrome Debugger|Chrome Debugger]]:
<pre>
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects
to the end user's experience. For more help, check http://xhr.spec.whatwg.org/.</pre>


It returns two values: .status = 0 or 200 if the file was read successfully and .responseText which has the entire contents of the file. If the file is not read successfully, a different status code is returned in .status.
It returns two values: .status = 0 or 200 if the file was read successfully and .responseText which has the entire contents of the file. If the file is not read successfully, a different status code is returned in .status.


There is no equivilent WriteFile. Use [[sql|Sql]], [[localstorage|LocalStorage]] or the PhoneGap API to save data.
There is no equivalent WriteFile. Use [[Sql]], [[LocalStorage]] or a [[Using_Cordova_Plugins|Cordova plugin]] to save data.


'''Returning the latest file
'''Returning the latest file contents
'''
'''


You can also pass a file location to ReadFile using this approach
You can also pass a file location to ReadFile using this approach


<pre>
Dim usrFile, req
Dim usrFile, req
usrFile = "users/12345.xml?&" & SysInfo(10)
usrFile = "users/12345.xml?" & SysInfo(10)
req = ReadFile(usrFile)
req = ReadFile(usrFile)
</pre>


This ?&" & SysInfo(10) at the end of the .xml ensures that the latest file on the server is returned. This is useful where you perhaps update the file and want to return the latest version later on within your application. Excluding this will only return the last cached file contents that you read.
The <code>?" & SysInfo(10)</code> at the end of the .xml ensures that the latest file on the server is returned. This is useful where you perhaps update the file and want to return the latest version later on within your application. Excluding this will only return the last cached file contents that you read.


== Example ==
== Example ==
Line 39: Line 48:
== Related Items ==
== Related Items ==


[[ajax|Ajax]]
* [[Ajax]]


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


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

Latest revision as of 14:18, 13 November 2020

ReadFile (filename[,method[, callBack]])

Description

Note: This function can be replaced by the Ajax() function.

ReadFile will read filename which can either be deployed with the app or be on the same server as the app. If the file is to be deployed with the app, include it in the manifest. This function will only work when deployed to a website: it will not work when deployed to a local folder.

Use method to define the access method. Usually this parameter can be left out: the default is Get. The next most common value for this is Post. Post responses are never cached, whereas Get responses can be. Post also allows larger data transfers. Other methods are Connect, Delete, Head, Options, Put, Trace, or Track. For more information on these options, look up XMLHttpRequest on the web. The function is based on XMLHttpRequest. See the ReadFile.nsx sample in Samples folder 7 "Ajax and ReadFile".

If you supply a callBack argument, a function with that name will be called when the read is complete. Adding this option will cause the operation to become asynchronous. The next statement after ReadFile will be executed immediately, before the read is complete. This results in smoother operation for the user, especially if the file is on a remote server.

If you do not use a callBack, you will see a warning in the Chrome Debugger:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects 
to the end user's experience. For more help, check http://xhr.spec.whatwg.org/.

It returns two values: .status = 0 or 200 if the file was read successfully and .responseText which has the entire contents of the file. If the file is not read successfully, a different status code is returned in .status.

There is no equivalent WriteFile. Use Sql, LocalStorage or a Cordova plugin to save data.

Returning the latest file contents

You can also pass a file location to ReadFile using this approach

Dim usrFile, req
usrFile = "users/12345.xml?" & SysInfo(10)
req = ReadFile(usrFile)

The ?" & SysInfo(10) at the end of the .xml ensures that the latest file on the server is returned. This is useful where you perhaps update the file and want to return the latest version later on within your application. Excluding this will only return the last cached file contents that you read.

Example

Rem ReadfileFile example
filename="g.txt"
req=ReadFile(filename)
If req.status=200 Then
  MsgBox req.responseText
Else
  MsgBox "File could not be read"
End If

Related Items