This extension uses the Dropzone.js open source file upload library to allow asynchronous file uploads in your form or ActiveForm.
Requirements ¶
Developed using Yii 1.1.13 but should work with older versions as well.
Usage ¶
Unzip the contents to your extensions folder.
In your view use it like this:
$this->widget('ext.dropzone.EDropzone', array(
'model' => $model,
'attribute' => 'file',
'url' => $this->createUrl('controller/action'),
'mimeTypes' => array('image/jpeg', 'image/png'),
'onSuccess' => 'someJsFunction();',
'options' => array(),
));
The options array accepts any option supported by the Dropzone.js library. Check the link below for all available options.
test
nice, dropzone extension
Small and clean
Nicely done. If you are planning any upgrades, let me suggest adding support for more events. IMHO error and complete are first candidates.
Updates
@sidewinder
Yes I will update it as soon as I can, kind of got some big projects these days.
Thanks for your suggestions!
option addRemoveLinks not work..
Hi, all..
I doing like this, but no addition button appear on preview... could anyone help...
<?php $this->widget('ext.dropzone.EDropzone', array( 'model' => $model, 'attribute' => 'file', 'url' => $this->createUrl('gallery/save_tempphoto',array('index'=>null)), 'mimeTypes' => array('image/jpg', 'image/png', 'image/gif'), 'options' => array('addRemoveLinks' =>true), ));?>
not working selection of types
bug in
'accept' => "js:function(file, done){if(jQuery.inArray(file.type,{$this->mimeTypes})){done('File type not allowed.');}else{done();}}",
incorrect use of jQuery.inArray(), should be
'accept' => "js:function(file, done){if(jQuery.inArray(file.type,{$this->mimeTypes})==-1){done('File type not allowed.');}else{done();}}",
@yan
fixed
looks good
looks good,but how to remove the uploaded file?
[Answer]How to remove uploaded file
You can simple update your CSS file from here copy and paste the content of dropzone.css
and the JS files from here
file name dropzone.js
how to make it reload?
Thanks for this great extension....
I want to ask something....
I combine this extension with a form.... when I use that form, I upload some image, and then I submit the form, if there is an error (validating error), page will come back to form.... how to reload image that I already upload? (without I upload again)
Thanks
Here its
Update your extension from the github link I set before, and set these attributes
if (count($model['ADV_PIC']) > 0) { for ($i = 0; $i < count($model['ADV_PIC']); $i++) { $mocks .= 'var mockFile' . $i . ' = {name: "' . $model['ADV_PIC'][$i] . '", size: ' . filesize(Yii::app()->params->imagesDir . $model['ADV_PIC'][$i]) . ', type: "image/jpeg"}; this.addFile.call(this, mockFile' . $i . '); this.options.thumbnail.call(this, mockFile' . $i . ', "' . Yii::app()->params->thumbsPath . $model['ADV_PIC'][$i] . '");'; $fields .= CHtml::hiddenField('picsers[]', $model['ADV_PIC'][$i]); } }
'init' => 'js:function(){' . $mocks . '}'
Translation?
I really like Dropzone a lot and also your extension making the implementation even more easier.
What I can't figure out so far, is why I can't translate the errors. Like the FileTooBig error, I tried:
....
'options' => array(
'dictFileTooBig'=>'To big stupid!',
....
Has no effect.
a litle tweak for delete
hello guys yesterday i download dropzone.js and update with the new one from github
you should do that.
on form section i create a hiddentext field
<?php echo CHtml::hiddenField('uploadId' , 'value', array('id' => 'hiddenInput')); ?> </div>
and on the widget i use the onSucccess function
<?php echo $form->labelEx($model,'file'); ?> <?php $this->widget('ext.dropzone.EDropzone', array( 'model' => $model, 'attribute' => 'file', 'url' => Yii::app()->request->baseUrl.'/api/insert', 'mimeTypes' => array('image/jpeg', 'image/png'), 'onSuccess' => 'succcesupload', 'options' => array('addRemoveLinks' =>true,), )); ?>
where the onSuccess function i updated with
$options = CMap::mergeArray(array( 'url' => $this->url, 'parallelUploads' => 1, 'paramName' => $this->name, 'accept' => "js:function(file, done){if(jQuery.inArray(file.type,{$this->mimeTypes}) == -1 ){done('File type not allowed.');}else{done();}}", 'init' => "js:function(){this.on('success',$this->onSuccess);}" ), $this->options);
where will return the object and id from fileupload controller such as id so i can remove and delete file that i upload before
function succcesupload(file ,response){ if($('#hiddenInput').val() == 'value'){ $('#hiddenInput').val(response); }else{ var before = $('#hiddenInput').val(); $('#hiddenInput').val(before +','+response); } var data = $('#hiddenInput').val(); var y = data.split(','); $(".dz-remove").each(function(i) { $(this).attr("value" , y[i++]); }); $(".dz-remove").click(function(){ var id = $(this).val(); var data = $('#hiddenInput').val(); var remove = data.split(','); remove = jQuery.grep(remove, function(value) { return value != id; }); $('#hiddenInput').val(remove.toString()); var token = '<?php echo Yii::app()->request->csrfToken; ?>'; var SiteUrl = '<?php echo Yii::app()->request->baseUrl; ?>'; $.ajax({ type: 'POST', url: SiteUrl+'/api/deleteupload', data : {YII_CSRF_TOKEN : token, fileId : id, type:'Avatar' }, success : function(){ $('.allert').show(); }, error : function(){ $('.error').show(); }, }); }); } </script>
and here my controller for saving uploads
public function actionInsert(){ if (!empty($_FILES)) { // Yii::app()->session['id_upload'] == ''; $random = rand(0,99999999999); $tempFile = $_FILES['avatar']['tmp_name']['file']; $targetPath = Yii::getPathOfAlias('webroot').'/images/avatar/'; $file_name = $random.'_'.str_replace(' ' ,'_' ,$_FILES['avatar']['name']['file']); $targetFile = $targetPath.$file_name; if(move_uploaded_file($tempFile,$targetFile)){ $upload = new SimpegUpload; $upload->file_name = $file_name; $upload->url = Yii::app()->request->baseUrl.'/images/avatar/'.$file_name; $upload->save(); $to_return = SimpegUpload::model()->findByAttributes(array('file_name'=>$file_name)); // var_dump($to_return->id); Yii::app()->session['id_upload'] = $to_return->id; echo $to_return->id; } } }
and controller for deleting uploads
public function actiondeleteupload(){ if(isset($_POST['fileId'])){ if($_POST['type'] == 'Avatar'){ $targetPath = Yii::getPathOfAlias('webroot').'/images/avatar/photos/'; } $data = SimpegUpload::model()->findbyPk($_POST['fileId']); if($data != null){ if(unlink($targetPath.$data->file_name)){ $data->delete(); } } } return true; }
hopes this working for you
and sorry for terible english
Ajax problem
Can not get to run the extension when the view containing the widget is loaded via Ajax. Not throw me any errors ...
Any help would be appreciated
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.