通过input = file上的onChange激活Sencha函数

时间:2012-12-12 07:50:28

标签: file extjs controller touch onchange

Sencha Touch不支持输入类型文件,因此我尝试使用某些html

config: {
    html: '<input type="file" onchange="readURL(this);">',

但我只能在我的应用程序之外调用函数..

有人知道我怎么称呼控制器功能吗?

1 个答案:

答案 0 :(得分:0)

它不是很漂亮,但它是一种解决方法,直到在Sencha Touch框架中实现此功能。这是我在视图文件中使用的代码

config: {
    html: '<input type="file" onchange="readURL(this);">',

一旦选择了文件,就会调用函数readURL。从这个函数我用我的应用程序\ controller \ function \调用以下代码

function readURL(input) { 
    window['myAppName'].app.getController('myControllerName').photoToStore(photo);
}

在photoToStore函数中,我得到了照片的base64

photoToStore: function(photo) {
    var reader = new FileReader();      
    var img    = new Image();
    reader.readAsDataURL(photo.files[0]);                       

    reader.onload = function(e) {   
        img.src = e.target.result;                  
    }
}

在最后一步中,我将base64 sting发送到后端,我可以将照片作为文件保存。