Class Storage#writeFile
storage
- Defined in: storage.js
| Constructor Attributes | Constructor Name and Description |
|---|---|
|
Storage#writeFile(successCallback, errorCallback, options)
Writes file
|
Class Detail
Storage#writeFile(successCallback, errorCallback, options)
Writes file
function writeFile() {
// This example will write binary data to file.
var successCb = function (cbObject){
console.log( "Successfully writen " + cbObject.written + " bytes" );
};
var failureCb = function(cbObject){
var errorCode = cbObject.errorCode;
var errorText = cbObject.errorText;
console.log( " Error Code [" + errorCode + "]: " + errorText);
};
//Read a file as binary with an ajax call.
var oReq = new XMLHttpRequest();
oReq.open("GET", '/my_file.data', true);
oReq.responseType = "arraybuffer";
oReq.onload = function (oEvent) {
var arrayBuffer = oReq.response; // Note: not oReq.responseText
if (arrayBuffer) {
var uint8View = new Int8Array(arrayBuffer);
var array = [];
for(var i=0;i < uint8View.length;++i) {
array[i] = uint8View[i];
}
// write data from the start, read the whole data, and write as binary
var options = {
data: array,
path: 'file://internal/data.dat',
position : 0,
mode :'truncate',
offset:0,
length : array.length,
encoding: 'binary'
};
var storage = new Storage();
storage.writeFile(successCb, failureCb, options);
}
};
oReq.send(null);
// This example will write text data to a file.
var successCb = function (cbObject){
console.log( "Successfully writen " + cbObject.written + " bytes" );
};
var failureCb = function(cbObject) {
var errorCode = cbObject.errorCode;
var errorText = cbObject.errorText;
console.log( " Error Code [" + errorCode + "]: " + errorText);
};
// write Text data, use utf-8 encoding, write all text.
var textData = "Hello SCAP!!!!!";
var options = {
data: textData,
path: 'file://internal/text.txt',
position : 0,
mode :'truncate',
offset:0,
length : textData.length,
encoding: 'utf8'
};
var storage = new Storage();
storage.writeFile(successCb, failureCb, options);
}
- Parameters:
- {Function} successCallback
- success callback function.
- {Function} errorCallback
- failure callback function.
- {Object} options
Property Type Description Required path Storage.SCAP_URI URI to the resource (internal/usb/sdcard). required data ArrayBuffer|String Data to write to the file. Data type should be ArrayBuffer if encoding is 'binary' or string otherwise. required mode String Write mode. Default is 'truncate'. 'truncate' : Truncate the file and start writing on it. File will be created if it doesn't exist. 'append' : Append to the file. File will be created if it doesn't exist. 'position' : Write from the position given by option.position. File must exist for this mode. optional position Number Position where this file will be written. Default value is 0. This option is valid only if mode is 'position'. If position is bigger than the existing file for positional write, the gap between the file contents and write position will be filled with '\00' character. optional length Number Write buffer length in bytes. Maximum buffer size is 10KB. Default value is data.length, or 10KB if data.length-offset > 10KB. optional encoding String Encoding to be applied to input data. Default value is 'utf8'. 'utf8' : input data is encoded as UTF-8. options.data should be a string. 'base64' : input data is encoded as base64. options.data should be a string. 'binary' : input data is encoded as raw binary. options.data should be an ArrayBuffer. optional offset Number Offset from buffer. Default value is 0. If offset+length > data.length, error will be returned. optional
- Since:
- 1.2
- Returns:
After the method is successfully executed, successCallback is called with the number of bytes written. The number of bytes written may be more than the length of the actual data. If an error occurs, errorCallback is called with errorCode and errorText. To see how error codes are defined, check Error.ERROR_CODE