如何在Yii中获取窗口小部件资产的绝对URL

时间:2014-02-26 21:32:07

标签: php yii uploadify

我想在Yii框架中创建一个上传多个文件的Widget(我使用的是uploadify js / swf插件)。所以我将我的widget类定义为:

<?php
class MultipleUploads extends CWidget {

public $assetsPath;

public $assetsDir;

public function init()
{
    parent::init();

    $this->assetsDir = $this->assetsPath ? $this->assetsPath : dirname(__FILE__) . '/assets/multipleUploads';
    $dir = Yii::app()->assetManager->publish($this->assetsDir);
    $cs = Yii::app()->clientScript;
    $cs->registerCoreScript('jquery');
    $cs->registerScriptFile($dir . '/jquery.uploadify.min.js', CClientScript::POS_HEAD);
    $cs->registerCssFile($dir . '/uploadify.css', CClientScript::POS_END);
}

public function run() {
    $this->render('multipleUploads/index', array('dir' => Yii::app()->getAssetManager()->publish($this->assetsDir)));
}
}
?>

,视图是:

<script type="text/javascript" src="http://localhost/arhitect/assets/be04804/jquery.uploadify.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://localhost/arhitect/assets/be04804/uploadify.css"/>

<h1>Upload files</h1>
<form>
    <div id="queue"></div>
    <input id="file_upload" name="file_upload" type="file" multiple="true">
</form>
<script type="text/javascript">
$(function() {
    $('#file_upload').uploadify({
        'formData'     : {
            'token'         : 'xxx',
            'action'        : 'upload'
        },
        'auto'              : false,
        'queueSizeLimit'    : 20,
        'removeCompleted'   : false,
        'swf'               : 'http://localhost/arhitect/assets/be04804/uploadify.swf',
        'uploader'          : '<?php echo Yii::app()->createAbsoluteUrl('project/index');?>',
        'fileTypeExts'      : '*.gif; *.jpg; *.jpeg; *.png',
        'fileSizeLimit'     : '10MB',
        'progressData'      : 'all',
        'onFallback'        : function() {
            alert('Flash was not detected.');
        },
        'onUploadSuccess'   : function(file, data, response) {
            console.log('response ' + response);
        },
        'onUploadError'     : function(file, errorCode, errorMsg, errorString) {
            console.log('The file ' + file.name + ' could not be uploaded: ' + errorString);
        },
        'onQueueComplete' : function(queueData) {
            parent.validate('refresh');
        }
    });
});

请注意视图文件中的硬编码路径(.js和.css文件):if m using the hardcoded path, the widget is rendered correctly. Is there any way to get the absolute url path to the assets widget文件夹?

感谢。

0 个答案:

没有答案