SWFUpload.php
2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/**
* SWFUpload CWidget
* @version 1.0.2 2010.10.19 14:08
*/
class SWFUpload extends CWidget
{
public $fileQuenueLimit = 10; // file quenue limit
public $imgUrlList =array();
public $uploadUrl = ''; //
public $postParams =array();
public $debug =false;
public $fileSizeLimit = 5; // file size limit 5MB
public $fileTypes = '*.jpg;*.jpeg;*.gif;*.png'; // uploaded file types
public $buttonText = '选择图片';
/**
* callbackJS是一个js函数
* 参数name为文件名,path为文件路径
* function(name,path)
* {
* }
* @var javascript function
*/
public $callbackJS =''/*'function(name,path){alert("文件名:"+name);alert("文件路径:"+path);}'*/;
public static $threadId = 0; //处理一个页面的多个上传widget,这里输入不同的参数
private $baseUrl = '';
public function init()
{
SWFUpload::$threadId ++ ;
if(is_string($this->imgUrlList))
{
$this->imgUrlList = array($this->imgUrlList);
}
if($this->uploadUrl === '')
{
$this->uploadUrl = $this->controller->createUrl('upload'); //上传路径默认action为upload ,可以使用SWFUploadAction
}
if(!is_bool($this->debug))
{
throw new Exception('debug参数需要一个bool值(false/true)');
}
$this->postParams['SWFUpload'] = SWFUpload::$threadId; //标识是SWFUpload上传请求
$this->postParams['callbackJS'] = $this->callbackJS;
$this->postParams['SessionID'] = Yii::app()->session->getSessionID();
$this->postParams['fileQuenueLimit'] = $this->fileQuenueLimit;
$assets=dirname(__FILE__).'/assets';
$this->baseUrl=Yii::app()->assetManager->publish($assets);
if(is_dir($assets))
{
Yii::app()->clientScript->registerCoreScript('jquery');
Yii::app()->clientScript->registerScriptFile($this->baseUrl.'/swfupload.js',CClientScript::POS_HEAD);
Yii::app()->clientScript->registerScriptFile($this->baseUrl.'/handlers.js',CClientScript::POS_HEAD);
}
else
{
throw new Exception('Upload - Error: Couldn\'t find assets to publish.');
}
}
public function run()
{
$this->renderFile(dirname(__FILE__).'/views/swfupload.php',array(
'fileQuenueLimit'=>$this->fileQuenueLimit,
'threadId'=>SWFUpload::$threadId,
'imgUrlList'=>$this->imgUrlList,
'uploadUrl' =>$this->uploadUrl,
'postParams' =>CJavaScript::jsonEncode((object)$this->postParams),
'debug' =>$this->debug?'true':'false',
'fileSizeLimit' => $this->fileSizeLimit,
'fileTypes' => $this->fileTypes,
'buttonText' => $this->buttonText,
'baseUrl' => $this->baseUrl,
));
}
}