Coco! Multi file Uploader Widget ¶
- INSTALL
- 1) GIT Cloning
- 2) Setup 'config/main'
- 3) Action Setup
- 4) Insert and configure the Widget
- 5) How to receive the uploaded file.
- 6) When your file is not received, or it always reports: "Failed"
- 7) Extra Options
by: Christian Salazar. christiansalazarh@gmail.com @yiienespanol, oct. 2012.
Ver la Documentación en Español
http://opensource.org/licenses/bsd-license.php
Requirement: Yii 1.1.11 a lower yii version than 1.1.12 produces an "CJavaScriptExpression" exception, please refer to issues about how to solve it.
Go to Coco Repository at Bit Bucket !
Try my new ajax uploader & user's file manager
- Single & Multi File Uploads via Ajax-jQuery
- Drag & Drop.
'Coco' is a widget for yii framework designed to handle File Uploads (Single and Multiple) using Ajax.
Using 'coco' is very simple, at first place you are required to setup a fixed action in any controller, this action serves for all every coco-widgets in your application.
At second place, you must insert the widget in any form. All the uploaded files will be stored in the path provided by widget attribute named: 'uploadDir', so this path must have write permissions for the user holding the www service. Common permissions are 777.
Coco takes its functionality from a very nice PHP library located at valums repository in github, all licences are explicit in coco related to Valums work. This is the same core code used in EAjaxUploader.
Go to the official valums google group.
INSTALL ¶
1) GIT Cloning ¶
cd /home/blabla/myapp/protected
mkdir extensions
cd extensions
git clone https://christiansalazarh@bitbucket.org/christiansalazarh/coco.git
When you aren't using GIT copy the entire 'coco' folder into your extensions folder
ABOUT DIRECT DOWNLOAD ! ¶
Please download the provided zip, and uncompress it into /protected/extensions/coco.
2) Setup 'config/main' ¶
'import'=>array(
'application.models.*',
'application.components.*',
'application.extensions.coco.*', // <------
),
3) Action Setup ¶
Connect the widget into your current web application using a fixed Action in siteController (or a distinct controller if you prefer). Only one action is required for all coco instances.
By default, using:
myapp/protected/controllers/SiteController.php
public function actions()
{
return array(
'captcha'=>array(
'class'=>'CCaptchaAction',
'backColor'=>0xFFFFFF,
),
'page'=>array(
'class'=>'CViewAction',
),
'coco'=>array(
'class'=>'CocoAction',
),
);
}
4) Insert and configure the Widget ¶
<?php
$this->widget('ext.coco.CocoWidget'
,array(
'id'=>'cocowidget1',
'onCompleted'=>'function(id,filename,jsoninfo){ }',
'onCancelled'=>'function(id,filename){ alert("cancelled"); }',
'onMessage'=>'function(m){ alert(m); }',
'allowedExtensions'=>array('jpeg','jpg','gif','png'), // server-side mime-type validated
'sizeLimit'=>2000000, // limit in server-side and in client-side
'uploadDir' => 'assets/', // coco will @mkdir it
// this arguments are used to send a notification
// on a specific class when a new file is uploaded,
'receptorClassName'=>'application.models.MyModel',
'methodName'=>'onFileUploaded',
'userdata'=>$model->primaryKey,
// controls how many files must be uploaded
'maxUploads'=>3, // defaults to -1 (unlimited)
'maxUploadsReachMessage'=>'No more files allowed', // if empty, no message is shown
// controls how many files the can select (not upload, for uploads see also: maxUploads)
'multipleFileSelection'=>true, // true or false, defaults: true
));
?>
5) How to receive the uploaded file. ¶
Coco invokes an specific method in your own class to notify you the uploaded file, this method is referenced by its name (the methodName widget attribute) and a specific class name (the receptorClassName widget attribute).
When a new file arrives from the view into your provided class then the Coco widget invokes this method and pass to it the 'userdata' argument and the full local path of the given uploaded file.
As an example, suppose you have /protected/models/MyModel.php and you need the uploaded file arrives in this class, so the widget config will be:
'receptorClassName'=>'application.models.MyModel',
'methodName'=>'myFileReceptor',
'userdata'=>$model->primaryKey,
Your class and method in MyModel would be:
class MyModel {
public function myFileReceptor($fullFileName,$userdata) {
// userdata is the same passed via widget config.
}
}
6) When your file is not received, or it always reports: "Failed" ¶
Check your php.ini for post_max_uploads variable, and please be consistent with 'sizeLimit'=>2000000.
Check log for errors. Coco will write an error log when something goes wrong.
7) Extra Options ¶
'buttonText'=>'Find & Upload',
'dropFilesText'=>'Drop Files Here !',
'htmlOptions'=>array('style'=>'width: 300px;'),
'defaultControllerName'=>'site',
'defaultActionName'=>'coco',
Hey
It would be great if you could pack source and upload it as a zip, or add demo page.
Cheers
hey there !, about "..pack source and upload it as a zip.."
this is done, please follow the link "go to repository", in this bitbucket page you'll find a "download" link near to: "Size 40.6 KB (download)".
the main reason to not provide a link here is because that link will point to an specific commit and not necesary to the most recent commit.
the demo is not required in this case, because this widget is very simple to implement: only put your widget in any view and put a static action into your siteController.php actions(). No more is required. To receive a notification when a file is uploaded please refer to step #5
Demo
Hi, bluyell, thanks for your ext!
I agree with lowlliet in the case of demo page. Sometimes we are just looking for an ext that fit some needs and download everything and setup it to see something in action is a time consuming task. I think that a simple page showing the widget in action would be very appreciated.
Thanks anyway!
:)
Regards!
Just started error.
I'm getting this:
include(CJavaScriptExpression.php) [function.include]: failed to open stream: No such file or directory
This is new to me. What am I missing?
Also, the send notification is handled in a model. I would have guessed it would be in a controller. Just curious on this.
First time trying - continued
Okay, I had to get the source for CJavaScriptExpression.php. I put it in framework/web/helper. But that didn't work. I put it in my protected/model and that got me past the "failed to open error".
Now I have:
CHttpException' with message 'The system is unable to find the requested action "coco&action=upload&nocache=1350505723755&data=a:6:{s:17:"allowedExtensions";a:4:{i:0;s:4:"jpeg";i:1;s:3:"jpg";i:2;s:3:"gif";i:3;s:3:"png";}
Here's my widget code (I have it in the form of one of my tables):
$this->widget('ext.coco.CocoWidget'
,array( 'id'=>'cocowidget1', 'onCompleted'=>'function(id,filename,jsoninfo){ }', 'onCancelled'=>'function(id,filename){ alert("cancelled"); }', 'onMessage'=>'function(m){ alert(m); }', 'allowedExtensions'=>array('jpeg','jpg','gif','png'), 'sizeLimit'=>2000000, 'uploadDir' => 'assets/', // please always remember to finish with an slash '/' // this arguments are used to send a notification // on a specific class when a new file is uploaded, // please configure and uncomment in order to receive // your uploaded file: 'receptorClassName'=>'application.models.MyModel', 'methodName'=>'myFileReceptor', 'userdata'=>$model->primaryKey, ));
about CJavaScriptExpression
helo, this is because you need Yii 1.11 at least. Please upgrade. If dont, you must download this class from yii framework 1.13 (or 12, or 11) directly from zip.
this class is a new way for using javascript expressions.
about "First time trying - continued"
this error occurs because maybe you dont configure the static action. Go to siteController.php and create it (step 3). If so, maybe a AccessControl rule is denying your request. Please ensure it first.
it is possible too you have an older yii version. please upgrade, or test it in a new yii project using a most recent yii version.
Usefull Extension
I tried it, it's really a usefull extension.
Just a note.
if you set:
'import'=>array( 'application.models.*', 'application.components.*', 'application.extensions.coco.*', // <------ ),
it should be unnecessary write:
$this->widget('ext.coco.CocoWidget' ......
to use:
$this->widget('CocoWidget' ......
should be enough.
thanks for your contribution,
Cheers
issue #1 SOLVED ! thanks for comments, enjoy it !
bug. Coco always allow uploads using any file extension no matter if is allowed or not in 'allowedExtensions'.
in file CocoWidget.php line number 179, please change:
$uploader = new ValumsFileUploader(array(), $this->sizeLimit);
for this:
$uploader = new ValumsFileUploader($this->allowedExtensions, $this->sizeLimit);
review the issue at:
review code changes to solve issue #1
I need function delete file.
May be user upload missing file So then I need function delete file.
Thank a lot for useful extension.>
I mean it can delete or remove file from the list before upload.
Widget for Manage images via Ajax
You can manage the uploaded images using this widget:
Imagegallery1 Ajax Image Manager
return error 404
Hi,
After upload completed, return error 404, the return url is:
http://site.com/index.php/coco/coco&action=upload&nocache=135597771...
I thinks must have a "?" after controller/action , I resolved this with put "?" after defaultActionName
I suggest to you, use one parameter for route and create url with createUrl yii function for create url from route and append data parameters.
Thanks
path file return to client
I do not understand why after uploading, the path files are stored on the server by Ajax should be sent to the client?
path "upload/"
fullpath "upload/filename.jpg"
I thinks isn't a security work.
Very IMPORTANT security bug!!!
I find a very important security bug in runAction function.
You use the parameters sent from client and set its for current object on server and save it, so If the user change this parameter for example change valid extensions by firebug and submit form, by easily can upload any file with any extensions on server!!!
double single quotation!
in the html output widget see:
<div id='cocowidget1' class='cocowidget''>
see double single quotation in end line!
don't check file size and file type on client side
Your widget don't use parameters(size, type,...) on client side and don't check it on client side! just send parameters to server and check it on server, this scenario is wrong and not fastest(because user must first upload and then check file type and valid extensions) and not secure (because any user can change valid file size or valid file extensions on client and upload any files!) !
to "Navy"
could you please write all this things in the proper issue location on repo ?
thanks for it.
to @navy, about: "Very IMPORTANT security bug!!!"
coco is based on a well and hard tested product: "the valums ajax file uploader", coco is a wrapper only, if coco is so bad and insecure then valums file uploader is the same too, and i think this is not the case.
Coco provides server side validation to check image type (based or not in mime) this is not the business for coco widget, is yours, coco only provide the uploaded file to your server.
to @navy, about: about 404.
this is because coco doesnt work with path based urls. in a short comming release i resolve this issue.
http://site.com/index.php/coco/coco&action=upload&nocache=135597771...
double single quotation!. SOLVED.
thanks for report it. it is solved and published.
to @navy, about: about: "path file return to client"
i dont understand your point, please create an issue in coco repo. thank u.
FIXED ISSUE: Nice Urls UrlManager
download or pull the new version, it has solved the issue about UrlManager (nice urls). Sorry for late.
maxUploads support (client-side)
this last commit has maxUploads support.
https://bitbucket.org/christiansalazarh/coco/get/0e06b51843c0.zip
New feature
Hi,
It's a very helpful and nice extension and I may have a suggestion to further improve it. It would be nice if you can add a delete button or an "x" link next to the uploaded file, in case the user changes his mind on the uploaded files.
PS: It would also be nice to add different icons before the file name preview.
In other words, here is a screenshot showing the new features
Screenshot
I hope you consider my suggestion :)
But anyway, +1 from me, it helped me a lot!
Cheers
About Delete button..
@Skylight,
yes you are in right. due to this extension is a big wrapper for its original valums file uploader, i must get involved in the internals of this author in order to provide this feature..possible consecuence: bug.
but, it is in mind to make this enhancement.
Awesome
@buyell, this is awesome news ... I do respect your work and I can imagine what it brings to improve/edit the valums code! I look forward to seeing it, and I think we all do :)
Keep doing the awesome job !
Cheers
Get file name
How to get only the file name you are uploading.
in response to: "How to get only the file name you are uploading."
use the callback.
'receptorClassName'=>'application.models.MyModel', 'methodName'=>'onFileUploaded', 'userdata'=>$model->primaryKey,
this class: "application.models.MyModel" must implements the "methodName", and will receive the uploaded file and the "userdata" argument.
class MyModel { public function onFileUploaded($fullFileName,$userdata) { // userdata is the same passed via widget config. } }
Cancel uploaded file (at least from the control)
After I upload a file (and let's say I'm allowing only 1 file upload)... the user decides, oh that's the wrong file I uploaded and needs to re-upload another file... How do I go about doing that. Because if he clicks on upload again... it'll say..you've reached max file uploads..
the only way would be to refresh the page.. i want to avoid that..
anyway thanks for saving valums from widen and giving us this extension :)
Bug
Cuando invoco más de un Coco en un formulario, sólo funciona el primero, el resto si sube el archivo pero no devuelve nada de userdata, ¿cómo soluciono esto?.
Impossible upload!
I got 404 error when I upload files, the documentation is poor!
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.