phonegap文件阅读器onloadend不起作用 [英] phonegap filereader onloadend doesn't work

查看:239
本文介绍了phonegap文件阅读器onloadend不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用phonegap拍照并将其放入img容器中。

I'm using phonegap to take a picture and put it into a img-container.

navigator.camera.getPicture(onSuccess, onFail);

var onSuccess =  function(imageURI) {
        var pic = document.getElementById('picture');
        pic.style.display = 'block';
        pic.src = imageURI;
};

var onFail = function(message) {
        $('#infoField').val(message);
};

这很完美。现在我想加载一个格式的图片,以便通过websocket发送它。因此我使用fileReader并将类型设置为dataUrl。

That works perfect. Now I want to load the picture with a format to be alble to send it over a websocket. Therefor I use the fileReader and set the type to dataUrl.

var reader = new FileReader();

reader.onloadend = function(evt) {
      $('textarea#textarea1').val("evt triggered");
      //var socket = io.connect(addr);
      //socket.emit('mobilePicture', "works");
};

reader.readAsDataURL(document.getElementById('picture').src);

不幸的是没有任何反应。无论如何不会触发onloadend事件。有什么想法?

Unfortunately nothing happens. The onloadend event isn't triggered anyhow. Any ideas?

推荐答案

尝试重新安排你的代码:

Try to re-arrange your code like this:

var onSuccess =  function(imageURI) {
        var pic = document.getElementById('picture');
        pic.style.display = 'block';
        pic.src = imageURI;

        var reader = new FileReader();

        reader.onloadend = function(evt) {
              $('textarea#textarea1').val("evt triggered");
              //var socket = io.connect(addr);
              //socket.emit('mobilePicture', "works");
        };

        reader.readAsDataURL(imageURI);
};

var onFail = function(message) {
        $('#infoField').val(message);
};

navigator.camera.getPicture(onSuccess, onFail);

您必须将文件对象传递给 file.reader 不仅仅是该文件的路径。

You have to pass a file object to the file.reader not just a path to the file.

编辑/更新:

要首先通过路径在文件系统上查找文件,您需要使用 fileSystem创建 fileEntry

Edit / update:
To find a file on your filesystem by a path first you will need to create a fileEntry using the fileSystem:

示例

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);

function gotFS(fileSystem) {
    fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, gotFileEntry, fail);
}

function gotFileEntry(fileEntry) {
    //fileEntry
    alert("Got the fileEntry!");
    reader.readAsDataURL(fileEntry);
    ...
}

这篇关于phonegap文件阅读器onloadend不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆