ebidel / idb.filesystem.js

HTML5 Filesystem API polyfill using IndexedDB
https://www.npmjs.com/package/idb.filesystem.js
Other
487 stars 46 forks source link

Can't open files in Firefox #8

Open marcoreni opened 11 years ago

marcoreni commented 11 years ago

I'm trying to build an webapp to open some text files saved in the filesystem. and I decided to use this library to be able to use my app on FF17b (next step will be try to add IndexedDb Shim to enable WebSQL fallback).

My code works fine in Chrome, but I receive an error when I try to open a file on Firefox.

Here is my code:

function onInitFs(fs) {
    fs.root.getFile('/path/to/file.txt', {create: false}, function(fileEntry) {
        fileEntry.file(function (file) {
            var reader = new FileReader();
            reader.onloadend = function (e) {
                //do something
            };
            reader.readAsText(file,'ISO-8859-1');
        },errorHandler);
    }, errorHandler);
}

and the error I receive is :NS_ERROR_FAILURE: Failure arg 0 [nsIDOMFileReader.readAsText] The line the error refers to is reader.readAsText(file,'ISO-8859-1');

I tried some debugging in Firebug and I discovered that object "file" has correct "name" but size 0, empty type and null "blob_", and nothing more.

Am I doing something wrong or is it a problem with the library?

ebidel commented 11 years ago

Can you attach a reproducible mini test case?

janotav commented 11 years ago

I encountered the same issue for empty files. To me it seems that this is the culprit:

  // If we're returning a zero-length (empty) file, return the fake file obj.
  // Otherwise, return the native File object that we've stashed.
  var file = this.file_.blob_ == null ? this.file_ : this.file_.blob_;

The problem is that "this.file_" is later passed to the reader and not recognized by it (it's not the native File representation). It doesn't make terrible sense to read empty file, but semantically such operation is correct and in my case the operation is performed by 3rd party code so I cannot avoid it either.

Eeems commented 8 years ago

I'm also running into this. I'm working around it for now by changing the line @janotav mentioned to the following.

var file = this.file_.blob_ == null ? new Blob([]) : this.file_.blob_;

There is probably a better way to do handling for this.