FileTransfer API
Original documentation: Cordova FileTransfer
Remark: Usage of cordova API needs http://tizen.org/privilege/filesystem.read privilege.
Remark: In order to access files, a proper privilege has to be defined additionally:
- for accessing only internal storage using this API, a privilege http://tizen.org/privilege/mediastorage must be provided,
- for accessing only external storage using this API, a privilege http://tizen.org/privilege/externalstorage must be provided,
- for accessing internal and external storage using this API, privileges (http://tizen.org/privilege/mediastorage and http://tizen.org/privilege/externalstorage) must be provided.
Since: 3.0
Table of Contents
- 1. Type Definitions
- 1.1. FileUploadParams
- 1.2. HeaderFields
- 1.3. FileDownloadOptions
- 2. Interfaces
- 2.1. FileUploadOptions
- 2.2. FileUploadResult
- 2.3. FileTransferError
- 2.4. FileTransfer
- 2.5. FileUploadSuccessCallback
- 2.6. FileDownloadSuccessCallback
- 2.7. FileTransferErrorCallback
- 3. Full WebIDL
Summary of Interfaces and Methods
Interface | Method |
---|---|
FileUploadOptions | |
FileUploadResult | |
FileTransferError | |
FileTransfer |
void upload (DOMString fileURL, DOMString server, optional FileUploadSuccessCallback? successCallback, optional FileTransferErrorCallback? errorCallback, optional FileUploadOptions? options, optional boolean? trustAllHosts)
void download (DOMString source, DOMString target, optional FileDownloadSuccessCallback? successCallback, optional FileTransferErrorCallback? errorCallback, optional boolean? trustAllHosts, optional FileDownloadOptions? options)
void abort ()
|
FileUploadSuccessCallback | void onsuccess (FileUploadResult result) |
FileDownloadSuccessCallback | |
FileTransferErrorCallback | void onerror (FileTransferError error) |
1. Type Definitions
1.1. FileUploadParams
typedef object FileUploadParams;
Since: 3.0
The key / value type to pass in the HTTP request.
2. Interfaces
2.1. FileUploadOptions
[Constructor(DOMString fileKey, DOMString fileName, DOMString mimeType, FileUploadParams params, HeaderFields headers, DOMString httpMethod)] interface FileUploadOptions { attribute DOMString fileKey; attribute DOMString fileName; attribute DOMString httpMethod; attribute DOMString mimeType; attribute FileUploadParams params; attribute boolean chunkedMode; attribute HeaderFields headers; };
Since: 3.0
Privilege level: public
Privilege: http://tizen.org/privilege/filesystem.read
Code example:
var fileURL; /* Assume the variable fileURL contains a valid URL to a text file on the device. */ var win = function(r) { console.log("Code = " + r.responseCode); console.log("Response = " + r.response); console.log("Sent = " + r.bytesSent); }; var fail = function(error) { alert("An error has occurred: Code = " + error.code); console.log("Upload error source " + error.source); console.log("Upload error target " + error.target); }; var options = new FileUploadOptions(); options.fileKey = "file"; options.fileName = fileURL.substr(fileURL.lastIndexOf("/") + 1); options.mimeType = "text/plain"; var params = {}; params.value1 = "test"; params.value2 = "param"; options.params = params; var ft = new FileTransfer(); ft.upload(fileURL, encodeURI("http://some.server.com/upload.php"), win, fail, options);
Output example:
Code = 200 Response = OK Sent = 1024
Constructors
-
Constructor (DOMString, DOMString, DOMString, FileUploadParams, HeaderFields, DOMString)
FileUploadOptions(DOMString fileKey, DOMString fileName, DOMString mimeType, FileUploadParams params, HeaderFields headers, DOMString httpMethod);
Since: 3.0
Code example:
/* Creates a FileUploadOptions objects */ var options = new FileUploadOptions("file", "doc.txt", "text/plain", null, null, "POST");
Attributes
-
DOMString fileKeyThe name of the form element. Defaults to file
Since: 3.0
Privilege level: public
Privilege: http://tizen.org/privilege/filesystem.read
-
DOMString fileNameThe file name to use when saving the file on the server. Defaults to image.jpg.
Since: 3.0
Privilege level: public
Privilege: http://tizen.org/privilege/filesystem.read
-
DOMString httpMethodThe HTTP method to use - either PUT or POST. Defaults to POST.
Since: 3.0
Privilege level: public
Privilege: http://tizen.org/privilege/filesystem.read
-
DOMString mimeTypeThe mime type of the data to upload. Defaults to image/jpeg.
Since: 3.0
Privilege level: public
Privilege: http://tizen.org/privilege/filesystem.read
-
FileUploadParams paramsA set of optional key/value pairs to pass in the HTTP request.
Since: 3.0
Privilege level: public
Privilege: http://tizen.org/privilege/filesystem.read
-
boolean chunkedModeWhether to upload the data in chunked streaming mode. Defaults to true.
Since: 3.0
Privilege level: public
Privilege: http://tizen.org/privilege/filesystem.read
-
HeaderFields headersA map of header name/header values. Use an array to specify more than one value.
Since: 3.0
Privilege level: public
Privilege: http://tizen.org/privilege/filesystem.read
2.2. FileUploadResult
[NoInterfaceObject] interface FileUploadResult { attribute long bytesSent; attribute long responseCode; attribute DOMString response; attribute HeaderFields headers; };
Since: 3.0
Privilege level: public
Privilege: http://tizen.org/privilege/filesystem.read
Attributes
-
long bytesSentThe number of bytes sent to the server as part of the upload.
Since: 3.0
Privilege level: public
Privilege: http://tizen.org/privilege/filesystem.read
-
long responseCodeThe HTTP response code returned by the server.
Since: 3.0
Privilege level: public
Privilege: http://tizen.org/privilege/filesystem.read
-
DOMString responseThe HTTP response returned by the server.
Since: 3.0
Privilege level: public
Privilege: http://tizen.org/privilege/filesystem.read
-
HeaderFields headersThe HTTP response headers by the server.
Since: 3.0
Privilege level: public
Privilege: http://tizen.org/privilege/filesystem.read
2.3. FileTransferError
interface FileTransferError { const short FILE_NOT_FOUND_ERR = 1; const short INVALID_URL_ERR = 2; const short CONNECTION_ERR = 3; const short ABORT_ERR = 4; const short NOT_MODIFIED_ERR = 5; attribute short code; attribute DOMString source; attribute DOMString target; attribute long http_status; attribute DOMString body; attribute DOMString _exception; };
Since: 3.0
Privilege level: public
Privilege: http://tizen.org/privilege/filesystem.read
Constants
Since: 3.0
Since: 3.0
Since: 3.0
Since: 3.0
Since: 3.0
Attributes
-
short codeOne of the predefined error codes listed above.
Since: 3.0
Privilege level: public
Privilege: http://tizen.org/privilege/filesystem.read
-
DOMString sourceURL to the source.
Since: 3.0
Privilege level: public
Privilege: http://tizen.org/privilege/filesystem.read
-
DOMString targetURL to the target.
Since: 3.0
Privilege level: public
Privilege: http://tizen.org/privilege/filesystem.read
-
long http_statusHTTP status code. This attribute is only available when a response code is received from the HTTP connection.
Since: 3.0
Privilege level: public
Privilege: http://tizen.org/privilege/filesystem.read
-
DOMString bodyResponse body. This attribute is only available when a response is received from the HTTP connection.
Since: 3.0
Privilege level: public
Privilege: http://tizen.org/privilege/filesystem.read
-
DOMString _exceptionEither e.getMessage or e.toString
Since: 3.0
Privilege level: public
Privilege: http://tizen.org/privilege/filesystem.read
2.4. FileTransfer
[Constructor()] interface FileTransfer { [TreatNonCallableAsNull] attribute ProgressCallback? onprogress; void upload(DOMString fileURL, DOMString server, optional FileUploadSuccessCallback? successCallback, optional FileTransferErrorCallback? errorCallback, optional FileUploadOptions? options, optional boolean? trustAllHosts); void download(DOMString source, DOMString target, optional FileDownloadSuccessCallback? successCallback, optional FileTransferErrorCallback? errorCallback, optional boolean? trustAllHosts, optional FileDownloadOptions? options); void abort(); };
Since: 3.0
Privilege level: public
Privilege: http://tizen.org/privilege/filesystem.read
Constructors
Attributes
-
ProgressCallback onprogress [nullable]Called with a ProgressEvent whenever a new chunk of data is transferred.
Since: 3.0
Privilege level: public
Privilege: http://tizen.org/privilege/filesystem.read
Code example:
var fileURL; /* Must be a valid url, such as cdvfile://localhost/persistent/path/to/file.txt. */ var win = function(r) { console.log("Success. File uploaded"); }; var fail = function(error) { /* error.code == FileTransferError.ABORT_ERR */ alert("An error has occurred: Code = " + error.code); console.log("Upload error source " + error.source); console.log("Upload error target " + error.target); }; var ft = new FileTransfer(); ft.onprogress = function(event) { console.log("Uploaded: " + event.loaded); }; ft.upload(fileURL, encodeURI("http://some.server.com/upload.php"), win, fail);
Output example:
Uploaded: 512 Uploaded: 1024 Success. File uploaded
Methods
-
upload
-
Sends a file to a server.
void upload(DOMString fileURL, DOMString server, optional FileUploadSuccessCallback? successCallback, optional FileTransferErrorCallback? errorCallback, optional FileUploadOptions? options, optional boolean? trustAllHosts);
Since: 3.0
Privilege level: public
Privilege: http://tizen.org/privilege/filesystem.read
Privilege: http://tizen.org/privilege/internet
Parameters:
- fileURL: Filesystem URL representing the file on the device. For backwards compatibility, this can also be the full path of the file on the device.
- server: URL of the server to receive the file, as encoded by encodeURI().
- successCallback [optional] [nullable]: A callback that is passed a FileUploadResult object.
- errorCallback [optional] [nullable]: : A callback that executes if an error occurs retrieving the FileUploadResult. Invoked with a FileTransferError object.
- options [optional] [nullable]: Optional parameters.
- trustAllHosts [optional] [nullable]: Optional parameter, defaults to false. If set to true, it accepts all security certificates.
Code example:
var fileURL; /* Must be a valid url, such as cdvfile://localhost/persistent/path/to/file.txt. */ var win = function(r) { console.log("Code = " + r.responseCode); console.log("Response = " + r.response); console.log("Sent = " + r.bytesSent); }; var fail = function(error) { alert("An error has occurred: Code = " + error.code); console.log("Upload error source " + error.source); console.log("Upload error target " + error.target); }; var ft = new FileTransfer(); ft.upload(fileURL, encodeURI("http://some.server.com/upload.php"), win, fail);
Output example:
Code = 200 Response = OK Sent = 1024
-
download
-
Downloads a file from a server.
void download(DOMString source, DOMString target, optional FileDownloadSuccessCallback? successCallback, optional FileTransferErrorCallback? errorCallback, optional boolean? trustAllHosts, optional FileDownloadOptions? options);
Since: 3.0
Privilege level: public
Privilege: http://tizen.org/privilege/filesystem.read
Privilege: http://tizen.org/privilege/filesystem.write
Parameters:
- source: URL of the server to download the file, as encoded by encodeURI().
- target: Filesystem url representing the file on the device. For backwards compatibility, this can also be the full path of the file on the device.
- successCallback [optional] [nullable]: A callback that is passed a FileEntry object.
- errorCallback [optional] [nullable]: A callback that executes if an error occurs when retrieving the FileEntry. Invoked with a FileTransferError object.
- trustAllHosts [optional] [nullable]: Optional parameter, defaults to false. If set to true, it accepts all security certificates.
- options [optional] [nullable]: Optional parameters, currently only supports headers (such as Authorization (Basic Authentication), etc).
Code example:
var fileURL; /* Must be a valid url, such as cdvfile://localhost/persistent/path/to/file.txt. */ var fileTransfer = new FileTransfer(); var uri = encodeURI("http://some.server.com/download.php"); fileTransfer.download(uri, fileURL, function(entry) { console.log("Download complete: " + entry.toURL()); }, function(error) { console.log("Download error source " + error.source); console.log("Download error target " + error.target); console.log("Upload error code" + error.code); }, false, {headers: {"Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="}});
Output example:
Download complete: file:///home/owner/apps_rw/Gk6hf8hjk/tmp/file.txt
-
abort
-
Aborts an in-progress transfer. The onerror callback is passed a FileTransferError object which has an error code of FileTransferError.ABORT_ERR.
void abort();
Since: 3.0
Privilege level: public
Privilege: http://tizen.org/privilege/filesystem.read
Privilege: http://tizen.org/privilege/internet
Code example:
var fileURL; /* Must be a valid url, such as cdvfile://localhost/persistent/path/to/file.txt. */ var win = function(r) { console.log("Should not be called"); }; var fail = function(error) { /* error.code == FileTransferError.ABORT_ERR */ alert("An error has occurred: Code = " + error.code); console.log("Upload error source " + error.source); console.log("Upload error target " + error.target); }; var ft = new FileTransfer(); ft.upload(fileURL, encodeURI("http://some.server.com/upload.php"), win, fail); ft.abort();
Output example:
An error has occurred: Code = 4 Upload error source file:///home/owner/apps_rw/Gk6hf8hjk/tmp/file.txt Upload error target http://some.server.com/file.txt
2.5. FileUploadSuccessCallback
[Callback=FunctionOnly, NoInterfaceObject] interface FileUploadSuccessCallback { void onsuccess(FileUploadResult result); };
Since: 3.0
Methods
-
onsuccess
-
Called when upload is successfully finished.
void onsuccess(FileUploadResult result);
Since: 3.0
Remark: See the example.
Parameters:
- result: A result object that defines an upload result.
2.6. FileDownloadSuccessCallback
[Callback=FunctionOnly, NoInterfaceObject] interface FileDownloadSuccessCallback { void onsuccess(FileEntry file); };
Since: 3.0
2.7. FileTransferErrorCallback
[Callback=FunctionOnly, NoInterfaceObject] interface FileTransferErrorCallback { void onerror(FileTransferError error); };
Since: 3.0
Methods
-
onerror
-
Called when file transfer is finished with an error
void onerror(FileTransferError error);
Since: 3.0
Remark: See the example.
Parameters:
- error: An object that defines details of the error.
3. Full WebIDL
module FileTransfer { typedef object FileUploadParams; typedef object HeaderFields; typedef object FileDownloadOptions; [Constructor(DOMString fileKey, DOMString fileName, DOMString mimeType, FileUploadParams params, HeaderFields headers, DOMString httpMethod)] interface FileUploadOptions { attribute DOMString fileKey; attribute DOMString fileName; attribute DOMString httpMethod; attribute DOMString mimeType; attribute FileUploadParams params; attribute boolean chunkedMode; attribute HeaderFields headers; }; [NoInterfaceObject] interface FileUploadResult { attribute long bytesSent; attribute long responseCode; attribute DOMString response; attribute HeaderFields headers; }; interface FileTransferError { const short FILE_NOT_FOUND_ERR = 1; const short INVALID_URL_ERR = 2; const short CONNECTION_ERR = 3; const short ABORT_ERR = 4; const short NOT_MODIFIED_ERR = 5; attribute short code; attribute DOMString source; attribute DOMString target; attribute long http_status; attribute DOMString body; attribute DOMString _exception; }; [Constructor()] interface FileTransfer { [TreatNonCallableAsNull] attribute ProgressCallback? onprogress; void upload(DOMString fileURL, DOMString server, optional FileUploadSuccessCallback? successCallback, optional FileTransferErrorCallback? errorCallback, optional FileUploadOptions? options, optional boolean? trustAllHosts); void download(DOMString source, DOMString target, optional FileDownloadSuccessCallback? successCallback, optional FileTransferErrorCallback? errorCallback, optional boolean? trustAllHosts, optional FileDownloadOptions? options); void abort(); }; [Callback=FunctionOnly, NoInterfaceObject] interface FileUploadSuccessCallback { void onsuccess(FileUploadResult result); }; [Callback=FunctionOnly, NoInterfaceObject] interface FileDownloadSuccessCallback { void onsuccess(FileEntry file); }; [Callback=FunctionOnly, NoInterfaceObject] interface FileTransferErrorCallback { void onerror(FileTransferError error); }; };