Skip to content

Instantly share code, notes, and snippets.

@junichi11
Created July 29, 2011 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junichi11/1114413 to your computer and use it in GitHub Desktop.
Save junichi11/1114413 to your computer and use it in GitHub Desktop.
CakePHP Plupload MeioUploadController
<?php
/**
* CakePHP Plupload Plugin
* Meioupload Controller
*
* Copyright (c) 2011 junichi11
*
* @author junichi11
* @license MIT LICENCE
*/
class MeiouploadController extends PluploadAppController {
public $name = 'Meioupload';
public $uses = array('Image');
public $helpers = array('Session', 'Plupload.Plupload');
public $components = array('Session', 'RequestHandler', 'Plupload.Plupload');
//===============================================
// action
//===============================================
/**
* meioupload upload
*/
public function upload(){
parent::upload();
$errors = null;
if(!empty($this->data) && $this->RequestHandler->isPost()){
$this->data['Image']['filename'] = $_FILES['file'];
if($this->Image->save($this->data)){
$result = '追加しました';
}else{
$errors = $this->validateErrors($this->Image);
$result = '追加できませんでした';
}
}
$this->set('filename', $_FILES['file']['name']);
$this->set('result', $result);
$this->set('errors', $errors);
}
/**
* meioupload widget
* @param string $ui {jquery | jqueryui}
*/
public function widget($ui = "jqueryui"){
$this->set('ui', $ui);
}
}
?>
<?php if(count($errors) > 0):?>
<?php foreach($errors as $type => $message ):?>
[<?php echo $message; ?>]
<?php endforeach;?>
<?php endif; ?>
<?php echo $filename ?> : <?php echo $result;?>
<?php $this->Plupload->loadAsset($ui);?>
<?php echo $this->Form->create(null, array('type' => 'post', 'action' => 'widget'));?>
<div id="uploader">
<p>You browser doesn't have Flash, Silverlight, Gears, BrowserPlus or HTML5 support.</p>
</div>
<?php echo $this->Form->end();?>
<h2>Uploaded Results</h2>
<div id="results" style="overflow: auto; height: 120px; border: solid 5px;"></div>
<script type="text/javascript">
$(function() {
$("#uploader").<?php echo ($ui == 'jquery') ? 'pluploadQueue' : 'plupload';?>(
<?php echo $this->Plupload->getOptions();?>
);
var $uploader = $('#uploader').plupload('getUploader');
// $uploader.bind('UploadComplete', function(up, files){
//
//// $('form').submit();
// });
$uploader.bind('FileUploaded', function(up, file, response){
$('#results').append('<p>' + response.response + '</p>');
});
$('form').submit(function($e) {
var $uploader = $('#uploader').plupload('getUploader');
if ($uploader.total.uploaded == 0) {
if ($uploader.files.length > 0) {
$uploader.bind('UploadProgress', function() {
if ($uploader.total.uploaded == $uploader.files.length)
$('form').submit();
});
$uploader.start();
} else{
alert('You must at least upload one file.');
}
$e.preventDefault();
}
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment