Uploading files using ajax (without flash).
Based on https://github.com/Widen/fine-uploader
Installation ¶
- Extract the release file under
protected/extensions/EFineUploader
Requirements ¶
- Yii 1.1 or above
Usage ¶
view:
<?$this->widget('ext.EFineUploader.EFineUploader',
array(
'id'=>'FineUploader',
'config'=>array(
'autoUpload'=>true,
'request'=>array(
'endpoint'=>'/files/upload',// OR $this->createUrl('files/upload'),
'params'=>array('YII_CSRF_TOKEN'=>Yii::app()->request->csrfToken),
),
'retry'=>array('enableAuto'=>true,'preventRetryResponseProperty'=>true),
'chunking'=>array('enable'=>true,'partSize'=>100),//bytes
'callbacks'=>array(
//'onComplete'=>"js:function(id, name, response){ }",
//'onError'=>"js:function(id, name, errorReason){ }",
),
'validation'=>array(
'allowedExtensions'=>array('jpg','jpeg'),
'sizeLimit'=>2 * 1024 * 1024,//maximum file size in bytes
'minSizeLimit'=>2*1024*1024,// minimum file size in bytes
),
)
));
?>
controller:
public function actionUpload()
{
$tempFolder=Yii::getPathOfAlias('webroot').'/upload/temp/';
mkdir($tempFolder, 0777, TRUE);
mkdir($tempFolder.'chunks', 0777, TRUE);
Yii::import("ext.EFineUploader.qqFileUploader");
$uploader = new qqFileUploader();
$uploader->allowedExtensions = array('jpg','jpeg');
$uploader->sizeLimit = 2 * 1024 * 1024;//maximum file size in bytes
$uploader->chunksFolder = $tempFolder.'chunks';
$result = $uploader->handleUpload($tempFolder);
$result['filename'] = $uploader->getUploadName();
$result['folder'] = $webFolder;
$uploadedFile=$tempFolder.$result['filename'];
header("Content-Type: text/plain");
$result=htmlspecialchars(json_encode($result), ENT_NOQUOTES);
echo $result;
Yii::app()->end();
}
Great
Great extension!
Differences between EAjaxUploader and EFineUploader
Can you explain, in brief, what is key difference between EAjaxUploader and EFineUploader? Is the only difference, a different library behind? Or maybe new EFineUploader offers some features that EAjaxUploader lacks?
Two troubles
Provided example does not work at all! I've copied your code line-by-line, without any modifications and after clicking
Upload a file
, nothing happend. When I looked into console, I saw an error in line1452
ofjquery.fineuploader-3.4.1.js
saying:Uncaught TypeError: Object [object Array] has no method 'onValidateBatch'
.You forgot to mention, that library behind your extension (Fine Uploader 3.4.1) is not free and requires a license for any kind of commercial use.
They have different features.
@Trejder
1) About license: If you are afraid of license claims - please do not use this extension.
2) They have different features. Both have example PHP code, if I remember correctly.
3) You may have done something wrong. Please show me your source code.
hi kosenka..
hi @kosenka, i copy paste all your sample above, and i change folder path and myname controller, but when i load pages that sown error :
PHP notice Undefined variable: postParams C:\xampp\htdocs\elibrary\public\protected\extensions\EFineUploader\EFineUploader.php(117) 117 $config['params']=$postParams;
help me please, thanks :d best regards.
Error: Undefined variable: postParams
@yiiadict
I have the same problem. My solution is just comment code at the line 117 (Extension/EFineUploader/EFineUploader.php):
$config['params']=$postParams;
then it's work fine.
show only
Hi when I include your code in my view I have a blank page, If I try to read html (output) I see only :
Please enable JavaScript to use file uploader.
why ?
[solved]
renderPartial issue :(
Chunking
Has anyone had success with the chunking feature? I've got it enabled as per the example, but it never posts the qqtotalparts or qqpartindex needed by the script. Any insights would be greatly appreciated!
no method 'onValidateBatch'
I have try your sample but i got this problem:
1) Line 117 commented
// $config['params']=$postParams;
2)commented
// mkdir($tempFolder, 0777, TRUE);
// mkdir($tempFolder.'chunks', 0777, TRUE);
because PHP warning mkdir(): File exists
3) commented
// $result['folder'] = $webFolder;
because PHP notice Undefined variable: webFolder
4) now, i see the component, but when i try to upload, i see nothing.
Into debug console i see javascript error:
Uncaught TypeError: Object [object Array] has no method 'onValidateBatch' jquery.fineuploader-3.4.1.js:1452
qq.FineUploaderBasic._isBatchValid jquery.fineuploader-3.4.1.js:1452
qq.FineUploaderBasic._uploadFileOrBlobDataList jquery.fineuploader-3.4.1.js:1409
qq.FineUploaderBasic.addFiles jquery.fineuploader-3.4.1.js:1049
qq.FineUploaderBasic._onInputChange jquery.fineuploader-3.4.1.js:1340
qq.UploadButton.onChange jquery.fineuploader-3.4.1.js:1117
(anonymous function) jquery.fineuploader-3.4.1.js:680
can you help me?
nothing happend...
hello,i face the same problem,
nothing is happened ,when i add a file ,
but its previous extension
eajaxupload
show the upload file details means,size,but failed
i am in stuck both of your extension....
please help me out.
Edit uploaded file names
Hi,
Currently am using this extension.Its great extension for me.
There is an option for edit the uploaded filenames in the "fineuploader" plugin.
So how to integrate this functionality in our EFineUploader extensions ?.
I have tried with "editFilename: { enabled: true } " this code.But nothing happens.
Please help me.
Thanks,
Thanks
Works fine :
1) in the controller, I commented the line
// $config['params']=$postParams;
2) in the view, I added these lines :
'callbacks'=>array( 'onComplete'=>"js:function(id, name, response){ // for test purpose alert(id + '/' + name + '/' + response.success + '/' + response.folder + '/' + response.filename); $('#efine_name').text(response.filename); }", //'onError'=>"js:function(id, name, errorReason){ }", 'onValidateBatch' => "js:function(fileOrBlobData) {}", // because of crash ),
and this one at the end of the view (just to make sure the uploaded filename is readable)
<div id="efine_name"></div>
Great Extension!
it is working, but is there any way to configure the button and label easily?
Great and working extension.
Thanks for the template, I was able to redesign the upload button and its label.
Lots of errors and mistakes in demo
As Patrice pointed out, demo & examples to this extension has a lot of errors, mistakes and bugs.
In particular:
onValidateBatch
event in your view (widget configuration) or jQuery script will fail,$config['params']=$postParams
line from the controller,$postParams
is undefined,$uploadedFile = $tempFolder.$result['filename'];
line from the controller, it is useless,$result['folder']
to readdirname($result['filename'])
, cause in current example it attempts to read a non existing variable,$tempFolder = Yii::getPathOfAlias('webroot').'/temp'; if(!file_exists($tempFolder) && !is_dir($tempFolder)) mkdir($tempFolder, 0777, TRUE); if(!file_exists($tempFolder.'/chunks') && !is_dir($tempFolder.'/chunks')) mkdir($tempFolder.'/chunks', 0777, TRUE);
or else your code will fail on attempting to create already existing folder. Alternatively you can add something like that:
rmdir($tempFolder.'/chunks'); rmdir($tempFolder);
to somewhere above
Yii::app()->end();
to remove temp folders yourself. But, in this case, you should of course move uploaded file out of this folder to some destination.FineUploader licensing
Keep in mind, that even though FineUploader (which stands behind this extension) is open source, it still require you to pay license fee, if you're going to use it in a commercial product!
Here is a cite from FineUploader FAQ:
The intent of our licensing model is to make Fine Uploader freely accessible to free open-source projects, while collecting money from commercial users. We put a good amount of time, resources, and money into this project, and the financial sustainability of the library is important in order for us to continue to provide quick support, new features, and maintenance. There are even more reasons for purchasing a license on our homepage.
So, think twice, before you start using this extension in any non-free project or you may quickly run into some serious licensing issues!
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.