LoginSignup
1
0

More than 3 years have passed since last update.

Cordova --- ファイルパスで BLOB を読み出す

Last updated at Posted at 2019-05-20

Platform

  • Cordova

Library

  • N/A

Usage

readFileDataFromPath.js
var filePath = 'myFilePath/myFile.zip';
var fileType = 'application/zip'; // *3 type-> MIME Types

window.resolveLocalFileSystemURL(filePath, (fileEntry) => {  // *1

    fileEntry.file((resFile) => {  // *1

        var reader = new FileReader();  // *2

        reader.onloadend = (evt) => {  // *2 callback when file completed load
            var blob = new Blob([evt.target.result], { type: new String(fileType) });
            resolve(blob);  // Return BLOB file
        };

        reader.onerror = (e) => {  // *2 callback when error occured
            reject(e);
        };

        reader.readAsArrayBuffer(resFile);  // *2 check *2 for more method to read file
    });
}, (error) =>{
    reject(error);
});

Reference

*1 https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/
*2 https://cordova.apache.org/docs/ja/3.1.0/cordova/file/filereader/filereader.html
*3 https://developer.mozilla.org/ja/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0