From 5a005426c09d4d5be6b345ac9f23d4758bc2a067 Mon Sep 17 00:00:00 2001 From: Lukasz Bardeli Date: Mon, 16 May 2016 10:10:48 +0200 Subject: [PATCH] [File] Added fileReader and fileWriter tutorials and guides Change-Id: Iadd09fa237f959180ee4182c0b6f1bd31b3ad2f2 Signed-off-by: Lukasz Bardeli --- org.tizen.guides/html/web/tizen/cordova/file_w.htm | 2 +- .../html/web/tizen/cordova/file_tutorial_w.htm | 204 ++++++++++++++++++++- 2 files changed, 202 insertions(+), 4 deletions(-) diff --git a/org.tizen.guides/html/web/tizen/cordova/file_w.htm b/org.tizen.guides/html/web/tizen/cordova/file_w.htm index 6835a07..2899173 100644 --- a/org.tizen.guides/html/web/tizen/cordova/file_w.htm +++ b/org.tizen.guides/html/web/tizen/cordova/file_w.htm @@ -94,7 +94,7 @@ See the Reading/writing files content

-Some description +Thanks FileReader and FileWriter operations you can read Blob objects in specific format (DataURL, Test, BinaryString, ArrayBuffer), and also you can create and write data to file. See the example.

diff --git a/org.tizen.tutorials/html/web/tizen/cordova/file_tutorial_w.htm b/org.tizen.tutorials/html/web/tizen/cordova/file_tutorial_w.htm index ab11bda..19fe9cf 100644 --- a/org.tizen.tutorials/html/web/tizen/cordova/file_tutorial_w.htm +++ b/org.tizen.tutorials/html/web/tizen/cordova/file_tutorial_w.htm @@ -103,18 +103,216 @@ The output:

Reading/writing files content

+
    +
  1. -Description. +To read file and return data as a base64-encoded data URL, you can use readAsDataURL method.

    -//////////////////// some code
    +var blob = new Blob(['abc']);
    +var fileReader = new FileReader();
    +fileReader.onload = function(){
    +    console.log('Loaded, result = '+fileReader.result);
    +};
    +fileReader.readAsDataURL(blob);
     

    The output:

    -///////////////// some output
    +Loaded, result = data:;base64,YWJj
    +
    +
  2. + +
  3. +

    +To read file and return data as a text, you can use readAsText method. +

    +
    +var blob = new Blob(['abc']);
    +var fileReader = new FileReader();
    +fileReader.onload = function(){
    +    console.log('Loaded, result = '+fileReader.result);
    +};
    +fileReader.readAsText(blob);
    +
    +

    +The output: +

    +
    +Loaded, result = abc
    +
    +
  4. + +
  5. +

    +To read file and return data as a binary and returns a binary string, you can use readAsBinaryString method. +

    +
    +var blob = new Blob(['abc']);
    +var fileReader = new FileReader();
    +fileReader.onload = function(){
    +    console.log('Loaded, result = '+fileReader.result);
    +};
    +fileReader.readAsBinaryString(blob);
    +
    +

    +The output: +

    +
    +Loaded, result = abc
    +
    +
  6. + +
  7. +

    +To read file and return data as an array buffer and result would be byte[], you can use readAsArrayBuffer method. +

    +
    +var blob = new Blob(['abc']);
    +var fileReader = new FileReader();
    +fileReader.onload = function(){
    +    resultValue = fileReader.result;
    +     console.log("Result: "+resultValue.toString()+' '+'ByteLength: '+resultValue.byteLength);
    +};
    +fileReader.readAsArrayBuffer(blob);
    +
    +

    +The output: +

    +
    +Result: [object ArrayBuffer]
    +ByteLength: 3
    +
    +
  8. + +
  9. +

    +To abort current operation of reading file, you can use abort method. +

    +
    +var blob = new Blob(['abc']);
    +var fileReader = new FileReader();
    +fileReader.onload = function(){
    +    console.log('Loaded');
    +};
    +fileReader.onabort = function(){
    +    console.log('aborted');
    +};
    +fileReader.readAsDataURL(blob);
    +fileReader.abort();
    +
    +

    +The output: +

    +
    +aborted
     
    +
  10. + +
  11. +

    +To writes data to the file, you can use write method. +

    +
    +successCallback = function(writer) {
    +   writer.onwrite = function(evt) {
    +        console.log('write success');
    +   };
    +   writer.write('some sample text');
    +};
    +errorCallback = function(err) {
    +    console.log(err.code);
    +};
    +// entry is FileEntry object retrieved by getFile() function of DirectoryEntry interface.
    +entry.createWriter(successCallback, errorCallback);
    +
    +

    +The output: +

    +
    +write success
    +
    +
  12. + +
  13. +

    +To moves the file pointer to the specified byte, you can use seek method. +

    +
    +successCallback = function(writer) {
    +   // fast forwards file pointer to the end of file.
    +   writer.seek(writer.length);
    +};
    +
    +errorCallback = function(err) {
    +    console.log(err.code);
    +};
    +
    +// entry is FileEntry object retrieved by getFile() function of DirectoryEntry interface.
    +entry.createWriter(successCallback, errorCallback);
    +
    +
  14. + +
  15. +

    +To short the file to the specified length, you can use truncate method. +

    +
    +successCallback = function(writer) {
    +writer.onwrite = function(evt) {
    +        console.log('truncate success');
    +   };
    +    writer.truncate(10);
    +};
    +
    +errorCallback = function(err) {
    +    console.log(err.code);
    +};
    +
    +// entry is FileEntry object retrieved by getFile() function of DirectoryEntry interface.
    +entry.createWriter(successCallback, errorCallback);
    +
    +

    +The output: +

    +
    +truncate success
    +
    +
  16. + +
  17. +

    +To abort writing the file, you can use abort method. +

    +
    +successCallback = function(writer) {
    +   writer.onwrite = function(evt) {
    +        console.log('write success');
    +   };
    +   writer.onabort = function(e){
    +         console.log('abort');
    +   };
    +   writer.write('some sample text');
    +   writer.abort();
    +};
    +
    +errorCallback = function(err) {
    +    console.log(err.code);
    +};
    +
    +// entry is FileEntry object retrieved by getFile() function of DirectoryEntry interface.
    +entry.createWriter(successCallback, errorCallback);
    +
    +

    +The output: +

    +
    +abort
    +
    +
  18. + +
-- 2.7.4