User Tools

Site Tools


en:code:data:web

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

en:code:data:web [2016/10/09 09:10]
en:code:data:web [2020/02/09 13:26] (current)
Line 1: Line 1:
 +====== Web ======
 +This object allows you to download data.
  
 +===== Functions =====
 +
 +==== Web ====
 +Constructor function.
 +
 +=== Syntax ===
 +<code javascript>​
 +var myObject = new Web(eventsAndOptions);​
 +</​code>​
 +
 +=== Arguments ===
 +  - eventsAndOptions - (object) the events that can be called
 +    * onFinished - ([[#​onFinished|onFinished]]) called when the data has been downloaded
 +    * onDownloadProgress - ([[#​onDownloadProgress|onDownloadProgress]]) called when the download progresses
 +    * onError - ([[#​onError|onError]]) called when an error has occured
 +    * file - ([[en:​code:​data:​file|File]] or string) the file or filename to use as a destination for the downloaded data, if this is not set the data will be stored into RAM
 +
 +=== Example ===
 +<code javascript>​
 +var myObject = new Web();
 +</​code>​
 +<code javascript>​
 +var myObject = new Web({
 + onFinished:​ function()
 + {
 + Console.print("​Finished!"​);​
 + },
 + file: "​myFile.txt"​
 +});
 +</​code>​
 +
 +===== Methods =====
 +
 +==== download ====
 +Start downloading.
 +
 +=== Syntax ===
 +<code javascript>​
 +myObject.download(url,​ options);
 +</​code>​
 +<code javascript>​
 +myObject.download(url);​
 +</​code>​
 +
 +=== Arguments ===
 +  - url - (string) the url of the resource to download
 +  - options - (object) options
 +    * rawHeaders - (object) raw headers to set
 +    * method - (Method) the method to use (get or post)
 +    * postData - (object) post data to send
 +    * query - (object) query items
 +    * user - (string) the username to use if asked
 +    * password - (string) the password to use if asked
 +
 +=== Returns ===
 +  * (Web) this Web
 +
 +=== Exceptions ===
 +  * (OpenFileError) unable to open the destination file
 +
 +=== Notes ===
 +This method is asynchronous,​ this means that it will return before the data is downloaded.
 +To know when this is done use the [[#​onFinished|onFinished]] event or the [[#​isDownloading|isDownloading]] method.
 +
 +==== isDownloading ====
 +Returns true if the data is still being downloaded.
 +
 +=== Syntax ===
 +<code javascript>​
 +myObject.isDownloading();​
 +</​code>​
 +
 +=== Returns ===
 +  * (boolean) true if the data is still being downloaded
 +
 +==== toImage ====
 +Returns the downloaded data as an [[en:​code:​core:​image|Image]].
 +
 +=== Syntax ===
 +<code javascript>​
 +myObject.toImage();​
 +</​code>​
 +
 +=== Returns ===
 +  * ([[en:​code:​core:​image|Image]]) the image
 +
 +=== Notes ===
 +You cannot use this method if you specified a destination file as no data is kept in memory.
 +
 +==== toText ====
 +Returns the downloaded data as text.
 +
 +=== Syntax ===
 +<code javascript>​
 +myObject.toText(encoding);​
 +</​code>​
 +<code javascript>​
 +myObject.toText();​
 +</​code>​
 +
 +=== Arguments ===
 +  - encoding - ([[en:​code:​core:​global#​encoding|Encoding]]) the encoding to use
 +
 +=== Returns ===
 +  * (string) the text
 +
 +=== Notes ===
 +You cannot use this method if you specified a destination file as no data is kept in memory.
 +
 +==== toRawData ====
 +Returns the downloaded data as [[en:​code:​core:​rawdata|RawData]].
 +
 +=== Syntax ===
 +<code javascript>​
 +myObject.toRawData();​
 +</​code>​
 +
 +=== Returns ===
 +  * ([[en:​code:​core:​rawdata|RawData]]) the raw data
 +
 +=== Notes ===
 +You cannot use this method if you specified a destination file as no data is kept in memory.
 +
 +==== cancel ====
 +Cancel the download.
 +
 +=== Syntax ===
 +<code javascript>​
 +myObject.cancel();​
 +</​code>​
 +
 +=== Returns ===
 +  * (Web) this Web
 +
 +===== Events =====
 +
 +==== onFinished ====
 +Called when the data has been downloaded.
 +
 +=== Syntax ===
 +<code javascript>​
 +myObject.onFinished = function() {};
 +</​code>​
 +
 +=== Example ===
 +<code javascript>​
 +myObject.onFinished = function()
 +{
 + //Event action
 +};
 +</​code>​
 +
 +==== onDownloadProgress ====
 +Called when the download progression has changed.
 +
 +=== Syntax ===
 +<code javascript>​
 +myObject.onDownloadProgress = function(bytesReceived,​ bytesTotal) {};
 +</​code>​
 +
 +=== Arguments ===
 +  - bytesReceived - (integer) received byte count
 +  - bytesTotal - (integer) total byte count
 +
 +=== Example ===
 +<code javascript>​
 +myObject.onDownloadProgress = function(bytesReceived,​ bytesTotal)
 +{
 + //Event action
 +};
 +</​code>​
 +
 +==== onError ====
 +Called when an error occurs.
 +
 +=== Syntax ===
 +<code javascript>​
 +myObject.onError = function(errorText) {};
 +</​code>​
 +
 +=== Arguments ===
 +  - errorText - (string) a text describing the error
 +
 +=== Example ===
 +<code javascript>​
 +myObject.onError = function(errorText)
 +{
 + //Event action
 +};
 +</​code>​
 +
 +===== Enumerations =====
 +
 +==== Method ====
 +The download method.
 +
 +=== Values ===
 +  - Get: use "​get"​ method (default)
 +  - Post: use "​post"​ method