使用Extjs4进行文件上传

5

我正在处理 Extjs4 文件上传控件。我有一个视图,其中包含文件上传控件,如下所示 -

Ext.define('Balaee.view.kp.dnycontent.Content', 
{
    extend:'Ext.form.Panel',
    requires:[
              'Balaee.view.kp.dnycontent.ContentView'
              ],
    id:'ContentId',
    alias:'widget.Content',
    enctype : 'multipart/form-data', 
    title:'This day in a history',
    items:[

    {
        xtype: 'fileuploadfield',
        hideLabel: true,
        emptyText: 'Select a file to upload...',
        //inputType: 'file',
        id: 'upfile',
        width: 220
}],
   buttons: [{
        xtype : 'button',
        fieldlabel:'upload',
        action:'upload',
        name:'upload',
        text: 'Upload',
        formBind:'true'

    }]
});

对应的控制器中的操作代码如下:

getUpload : function() {

        var file10 = Ext.getCmp('ContentId').getEl().down('input[type=file]').dom.files[0];

        var reader = new FileReader();
        reader.onload = function(oFREvent) {
            fileobj=oFREvent.target.result;
            console.log(oFREvent.target.result);

        };
        }
    });

以上控制器的功能是检索上传的文件并在读取器的onload函数中以编码格式显示它。即,“console.log(oFREvent.target.result);”行在控制台中以编码格式显示上传文件的数据。我需要将此文件发送到服务器端。因此,我将上述fileobj作为参数传递以存储为 -

var storeObj=this.getStore('kp.DnycontentStore');
         storeObj.load({
         params:{
         data:fileobj
         },
         callback: function(records,operation,success){
           console.log("send");
         },
         scope:this
         })

但是在reader.onload函数外部,它显示fileobj未定义。那么如何将此文件及其内容发送到服务器端?是否有其他方法在控制器中获取上传的文件并将其发送到服务器?请问有人可以指导我吗。


为什么不直接提交表单呢? - dbrin
谢谢您的答复。当我尝试通过添加以下代码来提交表单时 - " buttons: [{ xtype:'button', fieldlabel:'upload',action:'upload',name:'upload',text:'Upload',formBind:'true', handler:function(){ var form = this.up('form').getForm(); if(form.isValid()){ form.submit({ url:'index.php/QuestionBank/Qbpaper/getFile', success:function(fp,o){ Ext.Msg.alert('Success','Your photo“'+ o.result.file +'”has been uploaded.');} });} }}]" - user1722857
看起来没问题,有什么问题吗? - dbrin
它报错为 - “您正在尝试解码无效的JSON字符串:” - user1722857
错误在哪里,服务器、客户端?是在哪个阶段出现的?上传还是返回? - dbrin
我的服务器端[PHP-Yii框架]代码如下-"$fileName = $_FILES['upfile']['name']; $fp = fopen($fileName, 'r'); $content = fread($fp, filesize($fileName)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()){ $fileName = addslashes($fileName); }"现在它给我报错-Ext.Error: You're trying to decode an invalid JSON String: PHP notice Undefined index: upfile "实际上,我不知道如何在服务器端访问此文件。请您能指导我吗?我将非常感激。 - user1722857
1个回答

1

非常感谢您,我会参考您提供的链接。 - user1722857

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接