【问题标题】:Eajaxupload shows "failed" everytimeAjaxupload 每次都显示“失败”
【发布时间】:2012-04-28 02:41:13
【问题描述】:

我正在尝试使用 eajaxupload 扩展来处理我网站中的文件上传。我完全按照here 的说明进行操作,但遇到了一个问题:每次上传有效文件时都失败(对无效文件的验证效果很好)。我用谷歌搜索并找到了一个可能的解决方案here,但我没有成功。 这是在我的控制器中:

public function actionUpload() {
    Yii::import("ext.EAjaxUpload.qqFileUploader");

    $folder='upload/';// folder for uploaded files
    $allowedExtensions = array("zip","rar","7z");//array("jpg","jpeg","gif","exe","mov" and etc...
    $sizeLimit = 5 * 1024 * 1024;// maximum file size in bytes
    $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
    $result = $uploader->handleUpload($folder);
    $return = htmlspecialchars(json_encode($result), ENT_NOQUOTES);

    $fileSize=filesize($folder.$result['filename']);//GETTING FILE SIZE
    $fileName=$result['filename'];//GETTING FILE NAME

    echo $return;// it's array
}

这是我的看法:

$this->widget('ext.EAjaxUpload.EAjaxUpload',
    array(
    'id'=>'uploadFile',
    'config'=>array(
           'action'=>$this->createUrl('ProjectController/upload'),
           'allowedExtensions'=>array("zip","rar","7z"),//array("jpg","jpeg","gif","exe","mov" and etc...
           'sizeLimit'=>5*1024*1024,// maximum file size in bytes
           //'minSizeLimit'=>10*1024*1024,// minimum file size in bytes
           //'onComplete'=>"js:function(id, fileName, responseJSON){ alert(fileName); }",
           //'messages'=>array(
           //                  'typeError'=>"{file} has invalid extension. Only {extensions} are allowed.",
           //                  'sizeError'=>"{file} is too large, maximum file size is {sizeLimit}.",
           //                  'minSizeError'=>"{file} is too small, minimum file size is {minSizeLimit}.",
           //                  'emptyError'=>"{file} is empty, please select files again without it.",
           //                  'onLeave'=>"The files are being uploaded, if you leave now the upload will be cancelled."
           //                 ),
           //'showMessage'=>"js:function(message){ alert(message); }"
          )
    )); 

如果我哪里出错了,请告诉我。

【问题讨论】:

    标签: ajax file-upload yii yii-extensions


    【解决方案1】:

    它对我有用:

    $folder=Yii::app() -> getBasePath() . "/../images/uploads/"

    我的目录是:

    “我的网站/图片/上传”

    【讨论】:

      【解决方案2】:
      1. 您确定您的项目中有“upload/”文件夹吗?
      2. 我的 $folder 变量:

        $folder = Yii::getPathOfAlias('webroot').'/upload/';
        

      【讨论】:

        【解决方案3】:

        它对我有用:

        $folder = Yii::app()->basePath . '/uploads/documents/';
        

        我的根文件夹在哪里:

        c:\wamp\www\protected\uploads\documents\
        

        查看文件:

        <?php $this->widget('ext.EAjaxUpload.EAjaxUpload',
                        array(
                                'id'=>'uploadFile',
                                'config'=>array(
                                    'action'=>Yii::app()->createUrl('documents/addDocumentActivitatByAjax').'/'.$modelActivitat->id,
                                    'allowedExtensions'=>array('jpeg','jpg','gif','png','txt','doc','docx','xls','xlsx','xml','ppt','pptx','pdf', 'vsd'),
                                    'sizeLimit'=>10*1024*1024,// Tamaño máximo del fichero en bytes (10M)
                                    'minSizeLimit'=>1024,// Tamaño minimo del fichero en bytes
                                    'onComplete'=>"js:function(id, fileName, responseJSON){ 
                                                                    if(responseJSON.success){ 
                                                                        var msg = JSON.stringify(responseJSON);
                                                                        addFlashMessage(msg);
                                                                        $('#documents-grid').yiiGridView.update('documents-grid'); 
                                                                    }
                                                                }", 
                                    ),
                        )); ?>
        

        控制器:

        /**
                 * Recibe por ajax un campo tipo 'file' y lo guarda como 'documents'
                 * @param id Recibido por GET como parametro es el valor asignado al campo 'id_relacio' del Document a crear
                 */
                public function actionAddDocumentActivitatByAjax(){
                    $id_relacio = isset($_GET['id'])? $_GET['id'] : null;
                    if(!empty($id_relacio)){
                        $this->addDocument(LEME_DOCUMENTS_TIPUS_RELACIO_ACTIVITAT, $id_relacio);
                    }else{
                        echo htmlspecialchars(json_encode(array('success'=>'false', 'error'=>'No s\'ha rebut el id de l\'activitat associada')), ENT_NOQUOTES);
                    }
                }
        
        /**
                 * Recibe por ajax un campo tipo 'file' y lo guarda como 'documents'
                 * @param tipus_relacio Id del tipo de relación que guardará el Document
                 * @param id Recibido como parametro es el valor asignado al campo 'id_relacio' del Document a crear
                 */
                public function addDocument($tipus_relacio, $id_relacio){
                    Yii::import("ext.EAjaxUpload.qqFileUploader");
        
                    $folder = Yii::app()->basePath.$this->carpetaContingut;
        //            $allowedExtensions = array('jpeg','jpg','gif','png','txt','doc','docx','xls','xlsx','xml','ppt','pptx','pdf', 'vsd');
        //            $sizeLimit = 25 * 1024 * 1024;// 25M
        
                    $uploader = new qqFileUploader();
                    $originalName = $uploader->file->getName();
                    $originalName = SettingHelper::eliminaAcentos($originalName);
                    // Guardamos el archivo con nombre 'MD5()'
                    $result = $uploader->handleUpload($folder);
        
                    if(!isset($result['error'])){
                        // Si se sube bien generamos el registro en documents y renombramos el archivo recien subido a 'idDocument-NombreArchivoSubido.ext'
        //                $result['fileSize'] = filesize($folder.$result['filename']);//GETTING FILE SIZE
                        $fileNameMD5=$result['filename']; // Nombre del fichero guardado en encriptado en MD5
                        $document = new Documents();
                        $document->tipus_relacio = $tipus_relacio; // ID referent al tipus de relacio segons la taula vinculada
                        $document->id_relacio = $id_relacio;    // ID del registre al que pertany
                        $document->path = $this->carpetaContingut;
                        if($document->save()){
                            // Renombramos el archivo recien subido con el nuevo nombre : 'idDocument-NombreArchivoSubido.ext'
                            $newName = $document->id.'-'.$originalName;
                            if(rename($folder.$fileNameMD5, $folder.$newName)){ 
                            // Si se renombra, hacemos update del 'document' con el nombre del archivo final
                                $document->url = $newName;
                                $document->save();
                                $return = array('success'=>'true', 'estat'=>'success', 'text'=> 'El fitxer "'.$newName.'" s\'ha guardat correctament.');
                            }
                        }else{ // si da error al generar el document eliminamos el fichero recién subido.
                            unlink($folder.$fileNameMD5);
                            $return = array('success'=>'false', 'error'=> 'Error al generar el registre "document" associat.');
                        }
                    }else{ //Si da error al subir el fichero
                        $return = $result;
                    }
        
                    echo CJSON::encode($return);
            }
        

        【讨论】:

        • 请为提问者提供更多详细信息
        【解决方案4】:

        您需要打开 php.ini 文件并进行以下更改:

        upload_max_filesize = 10M //Change to upload_max_filesize = 20M
        

        post_max_size = 2M // Change to 50M
        

        重启 Apache 服务器就可以了!

        【讨论】:

          猜你喜欢
          • 2015-02-23
          • 2019-02-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-28
          相关资源
          最近更新 更多