File API or any other solution to read local files?

476 views
Skip to first unread message

hiroshi3110

unread,
May 19, 2011, 1:19:44 AM5/19/11
to phantomjs
Hi,

I'm trying to read local file contents with phantomjs.
I attempted following script, using File API's FileReader.

========= fileread.js =========
if (phantom.state.length === 0) {
phantom.state = 1;
phantom.content = '<html><body><form><input type="file" id="foo" /></
form></body></html>';
} else {
var foo = document.getElementById('foo');
phantom.setFormInputFile(foo, 'fileId');
var file = foo.files[0];
var reader = new FileReader();
reader.readAsText(file, "UTF-8");
reader.onload = function (evt) {
console.log(evt.target.result);
}
reader.onerror = function (evt) {
console.log("error reading file");
}
phantom.exit();
}
===========================

but, I got the error.

$ phantomjs test.js --upload-file fileId=test.js
undefined:8 ReferenceError: Can't find variable: FileReader

Current version of QtWebKit doesn't seem to have FileReader.
I hope that issue #31 - "Bleeding-edge WebKit" will solve this in the
future.

Are there any way to read local files?

Ivan De Marino

unread,
May 19, 2011, 2:45:29 AM5/19/11
to phan...@googlegroups.com
At the moment there is no support for the FileAPI.

If I get a second today, I'll check if they are part of QtWebKit 2.2: the next release wil contain it.
And I'd like to see FileAPI in there ;)

hiroshi saito

unread,
May 19, 2011, 11:21:26 AM5/19/11
to phan...@googlegroups.com
Ivan,
Thanks for your reply.

As a workaround, I use ajax requests to a small http server to
delegate local file access in my project:
https://github.com/hiroshi/pjs2png

Does anyone have any better idea?

Reply all
Reply to author
Forward
0 new messages