lilisheng33 2012-10-11 14:17:28 4065次浏览 2条回复 0 0 0

刚学YII不久。现在根据YII的官网教程,想自己封装个uploadify使用。 使用中,图片是上传到指定的地方,不过回调函数没有触发。

uploadify的代码

class llsUploadify extends CInputWidget{
    
    public $options = array();
    public function run()
    {
        $assetsFolder=Yii::app()->assetManager->publish(
            Yii::getPathOfAlias('application.components.assets.uploadify')//定义uploadify路径
        );
        Yii::app()->clientScript->registerCssFile($assetsFolder.'/uploadify.css');//加载CSS文件
        Yii::app()->clientScript->registerScriptFile($assetsFolder.'/jquery.uploadify.min.js');//加载js文件
        Yii::app()->clientScript->registerCoreScript('jquery');

        $options= CJavaScript::encode(array_merge(
            array(
                //'auto'=>false,//是否自动上传
                'swf'  => $assetsFolder.'/uploadify.swf',//flash文件
                //'uploader'    => $assetsFolder.'/uploadify.php',//上传处理文件
                'uploader'    => '/newblog/admin/upload/uploads.html',
                //'multi'=>true,  //是否多文件上传
                //'method'=>'post', //指定传输方式
                //'width'=>309, //按钮的宽度
                //'height'=>50,  //按钮的高度
                //'buttonText'=>'选择', //按钮上的文字
                //'fileObjName'=>'files',//设置$_FILES[这个名]
                //'buttonCursor'=>'arrow',//鼠标停留在按钮上的形状
                //'fileSizeLimit'=>'800KB',//设置最大上传大小 KB MB GB
                ////'queueID'=>'schedule', //队列放到指定的位置
                //'fileTypeDesc'=>'Image Files', //可选文件说明 在选择框色右下角出现 Image Files(图片文件)
                //'fileTypeExts'=>'*.gif; *.jpg; *.png', //规定能够上传的文件
                //'uploadLimit'=>5,  //同时能上传多小个文件 最大99
                //'removeCompleted'=>true, //是否保存队列
                //'removeTimeout'=>0, //文件上传完成后,队列多小时间消失
                ////'successTimeout'=>5, //等待服务器响应的时间
                //'progressData'=>'speed', //‘percentage’ or ‘speed’百分比 速度
                'onUploadSuccess'=> "function(file,data,response){alert(1)}",
            ),
            $this->options
        ));


        $jsUploadify ="
        'swf'  : $assetsFolder.'/uploadify.swf',//flash文件
        'uploader'    : '/newblog/admin/upload/uploads.html',
        'onUploadSuccess':function(file, data, response){
            alert(1)
        },
        ";


        if (isset ($htmlOptions['id']))
            $id= $htmlOptions['id'];
        else
            $id= CHtml::activeId($this->model, $this->attribute);

        Yii::app()->clientScript->registerScript($id,"$(document).ready(function() { $('#$id').uploadify($options);});");

        echo CHtml::activeFileField($this->model, $this->attribute, $this->htmlOptions);

    } 
    
}

调用的

<?php $this->widget('llsUploadify', array('model'=>$model, 'attribute'=>'file','htmlOptions'=>array('id'=>'News_file'))); ?>

处理上传的

public function actionUploads() { 
    $year=date('Y'); 
    $month=date('m'); 
    $path='uploads/'.$year.'/'.$month.'/'; 

    if(!file_exists('uploads/'.$year.'/')){ 
        mkdir('uploads/'.$year.'/','0777'); 
    }

    if(!file_exists($path)){ 
        mkdir($path,'0777'); 
    } 
    $filedata=$_FILES['Filedata']; 
    $fileName = uniqid().'.gif';
    @move_uploaded_file($filedata['tmp_name'],$path.uniqid().'.gif');
    echo 1;
}

http://www.yiiframework.com/wiki/203/how-to-create-a-wrapper-for-a-js-library

  • 回复于 2012-10-11 16:25 举报

    这个函数没有触发,应该就是没有上传成功,看看报什么错误?'onUploadSuccess'=> "function(file,data,response){alert(1)}"

  • 回复于 2012-10-11 17:28 举报

    上传到指定地方了。。

    [attach]953[/attach]

    uploadify的debug显示也是成功。返回的数据也有。。就是那个触发不了

    SWF DEBUG: 
    SWF DEBUG: Event: fileDialogStart : Browsing files. Multi Select. Allowed file types: *.*
    SWF DEBUG: Select Handler: Received the files selected from the dialog. Processing the file list...
    SWF DEBUG: Event: fileQueued : File ID: SWFUpload_0_0
    SWF DEBUG: Event: fileDialogComplete : Finished processing selected files. Files selected: 1. Files Queued: 1
    SWF DEBUG: StartUpload: First file in queue
    SWF DEBUG: Event: uploadStart : File ID: SWFUpload_0_0
    SWF DEBUG: ReturnUploadStart(): File accepted by startUpload event and readied for upload.  Starting upload to /newblog/admin/upload/uploads.html for File ID: SWFUpload_0_0
    SWF DEBUG: Event: uploadProgress (OPEN): File ID: SWFUpload_0_0
    SWF DEBUG: Event: uploadProgress: File ID: SWFUpload_0_0. Bytes: 407443. Total: 407443
    SWF DEBUG: Event: uploadSuccess: File ID: SWFUpload_0_0 Response Received: true Data: 1
    SWF DEBUG: Event: uploadComplete : Upload cycle complete.
    
您需要登录后才可以回复。登录 | 立即注册