vendredi 2 janvier 2015

How do you read a fileEntry passed from file handler in Chrome?


The app that I'm working on is set to open files from the Chrome file manager.


Here are my file handlers:



"file_handlers":{
"any":{
"extensions":["png","gif","webp"]
}


My app can then be selected to open files with the above extensions from the Chrome Files App.


My issue is reading the files. When the files are passed to my app, they are not files. They are file ENTRIES. Using a FileReader() does not work!


I found the following code from the API documentation:



var chosenFileEntry = null;

chooseFileButton.addEventListener('click', function(e) {
chrome.fileSystem.chooseEntry({type: 'openFile'}, function(readOnlyEntry) {

readOnlyEntry.file(function(file) {
var reader = new FileReader();

reader.onerror = errorHandler;
reader.onloadend = function(e) {
console.log(e.target.result);
};

reader.readAsText(file);
});
});
});


The above snippet prompts the user to select a file. From the above code I notice that there is a "file" method for the readOnlyEntry. That method does not exist for these entries that are passed to my app. Here is a screenshot of an inspection of the FileEntry object:


Screenshot


There is no "file" method, and FileReader() does not work because the fileEntry is not a Blob.


Here is my background.js:



chrome.app.runtime.onLaunched.addListener(function(launchData){
chrome.app.window.create('new.html',{
"id":"main",
"outerBounds":{
"minWidth":Math.round(window.screen.availWidth/2),
"minHeight":Math.round(window.screen.availHeight/2)
}
},function(createdWindow){
createdWindow.contentWindow.files = launchData.items;
});
});


How can you read a fileEntry without having to prompt the user? I want these files to open with the app.





Aucun commentaire:

Enregistrer un commentaire