【问题标题】:Extjs+PHP-File upload error- trying to decode an invalid JSON String:Extjs+PHP-文件上传错误-试图解码无效的 JSON 字符串:
【发布时间】:2013-04-12 19:08:58
【问题描述】:

我正在使用 extjs+PHP[yii 框架]。我正在研究文件上传控制。我有查看代码-

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: You're trying to decode an invalid JSON String: PHP 通知 未定义索引:upfile" 所以在 php 中如何访问通过 extjs4 发送的文件。请帮帮我

【问题讨论】:

    标签: php file-upload extjs4


    【解决方案1】:
    name:'file'
    

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

    id 是内部的,但name 是提交的标签。

    试试看。使用:

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

    代替:

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

    【讨论】:

    • 感谢先生帮助我。我按照你说的做了修改。由于此更改,php 错误已被删除。但它仍然给我错误-“Ext.Error:您正在尝试解码无效的JSON字符串:successerror”所以我需要做些什么改变。请指导我
    • php.net/manual/en/features.file-upload.php $_FILES['file']['name'] 不是服务器上的实际文件,而您却没有t used move_uploaded_file()`。我建议你阅读手册。
    • 另外,ExtJs 工作正常,但您的服务器端需要维修。
    • 非常感谢先生帮助我。我指的是你提供的链接。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-30
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多