This extension adds a flash based file-upload with progress-bar and multi-file upload capability. It implements all options of Uploadify (http://www.uploadify.com)
Note: the widget defaults to a single file auto-upload widget showing a single button with 'Browse' caption.
The only required options are:
name - name of the widget
script - the controller action to call when the file(s) are received.
protected/extensionsSee http://www.uploadify.com/download/ for a full description of all options
See the following code example:
$this->widget('application.extensions.uploadify.EuploadifyWidget', array( 'name'=>'uploadme', 'options'=> array( //'uploader' => '/js/uploadify.swf', 'script' => $this->createUrl('test/UploadedFiles'), 'cancelImg' => '/js/cancel.png', 'auto' => true, 'multi' => false, 'folder' => '/tmp', 'scriptData' => array('extraVar' => 1234, 'PHPSESSID' => session_id()), //'fileDesc' => 'Declaratiebestanden', //'fileExt' => '*.*', 'buttonText' => 'Upload bestanden', 'buttonImg' => '/images/upload.gif', 'width' => 150, ), 'callbacks' => array( 'onError' => 'function(evt,queueId,fileObj,errorObj){alert("Error: " + errorObj.type + "\nInfo: " + errorObj.info);}', 'onComplete' => 'function(){alert("Complete");}', 'onCancel' => 'function(evt,queueId,fileObj,data){alert("Cancelled");}', ) ));
And put the following code in your controller:
public function actionUploadedFiles() { // flash does NOT pass the session // thus we pass the id with a $_POST variable Yii::app()->session->sessionID = $_POST['PHPSESSID']; Yii::app()->session->init(); // Do whatever you need to do with the files you just received $files = var_export($_FILES, true); $this->log('Files ' . $files); echo 1; }
Total 11 comments
Using json_encode for the self::encode() method is a good call, except for two things: - There are Yii classes that wrap this, best to stick to framework calls. - This will encode the callbacks as strings.
This is best now as:
sorry for wrong code fixed below:
elseif($k == 'scriptData') { /* $sd =''; foreach($v as $dkey=>$dval) { $sd .= "'" . $dkey ."':'" . $dval ."',"; } $es[] = "'" . $k . "':{" . $sd . "}"; */ $es[] = "'" . $k . "':".json_encode($v);I use version 1.0.1, still can not use in IE. why not use json_encode, it is easy and correct.
/* elseif($k == 'scriptData') { $sd =''; foreach($v as $dkey=>$dval) { $sd .= "'" . $dkey ."':'" . $dval ."',"; } $es[] = "'" . $k . "':{" . $sd . "}"; */ $es[] = "'" . $k . "':".json_encode($v);Anyone had any luck getting this working with CSRF validation? I've tried passing the CSRF token in the scriptData array as well as the session id but it's failing with message 'The CSRF token could not be verified.'
file "UploadifyWidget.php" at line 221 change to:
private static function encode($value) { return json_encode($value); }You can use this extension with CUploadedFile and CFileValidator by specifying Uploadify's fileDataName property to match what CHtml::activeFileField() generates for the file inputs name attribute.
View file snippet:
$this->widget('application.extensions.uploadify.EuploadifyWidget', array( 'model' => $image, 'attribute' => Image::getFileAttributeName(), 'options'=> array( 'fileDataName' => 'Image[' . Image::getFileAttributeName() . ']', // wont work with out this 'script' => $this->createUrl('image/upload'), 'scriptData' => array('PHPSESSID' => session_id()), 'auto' => true, 'multi' => true, 'fileDesc' => 'Image Files', 'fileExt' => Image::getUploadifyFileTypes(), 'queueSizeLimit' => Image::getMaxQueueSize(), 'sizeLimit' => Image::getMaxFileSizeBytes(), 'buttonText' => 'Browse Images', 'width' => 110, 'height' => 25, ), 'callbacks' => array() ));queueId造成队列框设置无效; protected $validOptions = array( 'queueId'=>array('type'=>'string'), 中'queueId'改为'queueID'即可
Jerry,
Fixed it in version 1.0.1 Thanks for the report
above line: 241 add newline use following:
$sd = substr($sd, 0, -1);
it will avert ie browser can not read array/object last comma ","
Looking forward to more work~
Leave a comment
Please login to leave your comment.