Extjs + PHP-File上传错误 - 尝试解码无效的JSON字符串:

时间:2013-04-12 11:01:42

标签: php file-upload extjs4

我在extjs + PHP [yii framework]工作。我正在进行文件上传控制。我有查看代码 -

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',
        name:'file',
        width: 220
}],

   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',
                    waitMsg: 'Uploading your photo...',
                  //  uploadPath:'/app/model',
                    success: function(fp, o) {
                        Ext.Msg.alert('Success', 'Your photo "' + o.result.file + '" has been uploaded.');
                    }
                });} } }]});

我写了上面的代码来从extjs4发送文件。我的服务器端是PHP [Yii框架]。对于服务器端,我编写了代码来接收此文件 -

public function actiongetFile()
    {
        $fileName = $_FILES['upfile']['name'];
        $fileSize = $_FILES['upfile']['size'];
        $fileType = $_FILES['upfile']['type'];
        $fp      = fopen($fileName, 'r');
        $content = fread($fp, filesize($fileName));
        $content = addslashes($content);
        fclose($fp);
        if(!get_magic_quotes_gpc()){
            $fileName = addslashes($fileName);
        }

但是服务器端代码无法正常工作。从extjs发送文件时,它给出错误 - “Ext.Error:您正在尝试解码无效的JSON字符串: PHP通知 未定义的索引:upfile“ 所以在php中如何访问通过extjs4发送的文件。请帮帮我

1 个答案:

答案 0 :(得分:0)

name:'file'

这是你在数组中的文件的索引。

id是内部的,但name是submition的标签。

试试吧。使用:

$_FILES['file']['name']

而不是:

$_FILES['upfile']['name']