callback function is not fired.
Opened this issue · 3 comments
Hi there,
Thanks a lot for nice extension and make it available in yii. My uploading works like charm, I am able to save data in database as well. However, I am stuck at this point.
the call back function is not getting fired. I am trying to write a location.reload() in callback function. here is the code I am using this code in my view in yii.
widget('application.extensions.Plupload.PluploadWidget', array( 'config' => array( 'runtimes' => 'gears,flash,silverlight,browserplus,html5', 'url' => $this->createUrl('customer/UploadFilesPlupload'), //'max_file_size' => '1mb', 'resize' => array('width' => 600, 'height' => 450, 'quality' => 180), 'unique_names'=> false, 'filters' => array( array('title' => Yii::t('app', 'Images files'), 'extensions' =>'jpg,jpeg,gif,png'), ), 'autostart' => false, 'jquery_ui' => false, 'reset_after_upload' => false, 'chunk_size' => '1mb', 'callbacks' => array( 'FileUploaded' => "function(up,file,response){alert('file uploaded');}", ), ), 'id' => 'uploader' )); ?>
Hi,
this is strange: please find below my currently working code.
The callback is called.
I use chunked uploads, but should not be too different if you don't use chunked uploads.
$this->widget('application.extensions.plupload.PluploadWidget', array(
'config' => array(
//'runtimes' => 'gears,flash,silverlight,browserplus,html5',
'runtimes' => Yii::app()->params['pluploadRuntimes'],
'url' => $this->createUrl('blob/uploadFilesPlupload', array(
'uniqid' => str_replace(".", "", uniqid("", TRUE)),
'company_id' => Yii::app()->user->model->company->id)),
//'max_file_size' => str_replace("M", "mb", ini_get('upload_max_filesize')),
'max_file_size' => Yii::app()->params['maxFileSize'],
'chunk_size' => '100kb',
'filters' => array(
array('title' => Yii::t('app', 'Documenti PDF'), 'extensions' => 'pdf'),
array('title' => Yii::t('app', 'Documenti Word'), 'extensions' => 'doc,docx'),
array('title' => Yii::t('app', 'Documenti Excel'), 'extensions' => 'xls,xlsx'),
),
'language' => Yii::app()->language."-documents",
//'max_file_number' => 1,
'autostart' => false,
'multiple_queues' => true,
'file_list_height' => 150,
'visible_header' => false,
'jquery_ui' => false,
'callback_total_queued' => 'function(num_files){
if (num_files != 0) {
jQuery("#submit_prev").prop("disabled", "disabled");
jQuery("#submit_next").prop("disabled", "disabled");
jQuery("#submit_prev").addClass("ui-state-disabled");
jQuery("#submit_next").addClass("ui-state-disabled");
} else {
jQuery("#submit_prev").removeProp("disabled");
jQuery("#submit_next").removeProp("disabled");
jQuery("#submit_prev").removeClass("ui-state-disabled");
jQuery("#submit_next").removeClass("ui-state-disabled");
}
}',
//'add_files_text' => Yii::t('app', 'Aggiungi documenti'),
),
'callbacks' => array(
'FileUploaded' => 'function(up,file,response){
//console.log("Document: " + response.response);
var resp = jQuery.parseJSON(response.response);
if (typeof resp.blob_id != "undefined") {
jQuery("#doc_list").html(jQuery("#doc_list").html() +
"<li><img src=\''.Yii::app()->baseUrl.'/images/arrow.png\' alt=\''.Yii::t('app', 'Sposta').'\' class=\'handle\' />" +
"<img src=\''.Yii::app()->baseUrl.'/images/delete.png\' alt=\''.Yii::t('app', 'Cancella').'\' class=\'button\' onclick=\'doclist_delete(jQuery(this));\' />" +
"<a href=\'" + resp.url + "\' target=\'_blank\'><img src=\''.Yii::app()->baseUrl.'/images/link.png\' alt=\''.Yii::t('app', 'Link').'\' /></a>" +
"<input type=\'text\' id=\'new_name_" + resp.blob_id + "\' name=\'new_name_" + resp.blob_id + "\' class=\'edit-in-place ui-corner-all\' value=\'" + strip_tags(resp.filename).replace("\'", "'") + "\'></li>");
'.//jQuery("#new_name_" + resp.blob_id).'.$editInPlaceScript.'
'}
}',
),
'id' => 'doc_uploader'
));
Hi there,
I have seen your code and it seems all the same. can you edit you code to show an alert.
Also, the last line of my plupload function in controller is this :
die(json_encode($ret));
Is this correct? Also, here is my code for view part, as mentioned in issue also:
widget('application.extensions.Plupload.PluploadWidget', array( 'config' => array( 'runtimes' => 'gears,flash,silverlight,browserplus,html5', 'url' => $this->createUrl('customer/UploadFilesPlupload'), //'max_file_size' => '1mb', 'resize' => array('width' => 600, 'height' => 450, 'quality' => 180), 'unique_names'=> false, 'filters' => array( array('title' => Yii::t('app', 'Images files'), 'extensions' =>'jpg,jpeg,gif,png'), ), 'autostart' => false, 'jquery_ui' => false, 'reset_after_upload' => false, 'callbacks' => array( 'FileUploaded' => 'function(up,file,response){var resp = jQuery.parseJSON(response.response);alert(resp);}', ), ), 'id' => 'uploader' )); ?>
From: enricodetoma notifications@github.com
To: enricodetoma/yii-plupload yii-plupload@noreply.github.com
Cc: vaibhavbhasin vaibhav.bhasin@yahoo.co.in
Sent: Thursday, 9 May 2013 12:49 PM
Subject: Re: [yii-plupload] callback function is not fired. (#2)
Hi,
this is strange: please find below my currently working code.
The callback is called.
I use chunked uploads, but should not be too different if you don't use chunked uploads.
$this->widget('application.extensions.plupload.PluploadWidget', array( 'config' => array( //'runtimes' => 'gears,flash,silverlight,browserplus,html5', 'runtimes' => Yii::app()->params['pluploadRuntimes'], 'url' => $this->createUrl('blob/uploadFilesPlupload', array( 'uniqid' => str_replace(".", "", uniqid("", TRUE)), 'company_id' => Yii::app()->user->model->company->id)), //'max_file_size' => str_replace("M", "mb", ini_get('upload_max_filesize')), 'max_file_size' => Yii::app()->params['maxFileSize'], 'chunk_size' => '100kb', 'filters' => array( array('title' => Yii::t('app', 'Documenti PDF'), 'extensions' => 'pdf'), array('title' => Yii::t('app', 'Documenti Word'), 'extensions' => 'doc,docx'), array('title' => Yii::t('app', 'Documenti Excel'), 'extensions' => 'xls,xlsx'), ), 'language' => Yii::app()->language."-documents", //'max_file_number' => 1, 'autostart' => false, 'multiple_queues' => true, 'file_list_height' => 150, 'visible_header' => false,
'jquery_ui' => false, 'callback_total_queued' => 'function(num_files){ if (num_files != 0) { jQuery("#submit_prev").prop("disabled", "disabled"); jQuery("#submit_next").prop("disabled", "disabled"); jQuery("#submit_prev").addClass("ui-state-disabled"); jQuery("#submit_next").addClass("ui-state-disabled"); } else { jQuery("#submit_prev").removeProp("disabled"); jQuery("#submit_next").removeProp("disabled"); jQuery("#submit_prev").removeClass("ui-state-disabled"); jQuery("#submit_next").removeClass("ui-state-disabled"); } }', //'add_files_text' => Yii::t('app', 'Aggiungi documenti'), ), 'callbacks' => array( 'FileUploaded' => 'function(up,file,response){ //console.log("Document: " + response.response); var resp = jQuery.parseJSON(response.response); if (typeof resp.blob_id != "undefined") { jQuery("#doc_list").html(jQuery("#doc_list").html() + "
/>" + "<img src=''.Yii::app()->baseUrl.'/images/delete.png' alt=''.Yii::t('app', 'Cancella').'' class='button' onclick='doclist_delete(jQuery(this));' />" + "<a href='" + resp.url + "' target='blank'><img src=''.Yii::app()->baseUrl.'/images/link.png' alt=''.Yii::t('app', 'Link').'' />" + "<input type='text' id='new_name" + resp.blob_id + "' name='new_name_" + resp.blob_id + "' class='edit-in-place ui-corner-all' value='" + strip_tags(resp.filename).replace("'", "'") + "'>
—
Reply to this email directly or view it on GitHub.
Hi,
sorry but I'm busy and I can't try the code with the alert, but I assure you that the callback is called.
Here is the relevant part in the controller:
public function actionUploadFilesPlupload()
{
// Needed to prevent javascript log output in the response
$this->disableWebLogRoute();
// HTTP headers for no cache etc
header('Content-type: text/plain; charset=UTF-8');
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
// Settings
$targetDir = Yii::app()->basePath . "/../tmp/plupload";
$maxFileAge = 24 * 60 * 60; // Temp file age in seconds
// 5 minutes execution time
@set_time_limit(5 * 60);
// usleep(5000);
// Get parameters
$chunk = isset($_REQUEST["chunk"]) ? $_REQUEST["chunk"] : 0;
$chunks = isset($_REQUEST["chunks"]) ? $_REQUEST["chunks"] : 0;
$originalName = isset($_REQUEST["name"]) ? $_REQUEST["name"] : '';
$uniquid = isset($_REQUEST["uniqid"]) ? $_REQUEST["uniqid"] : '';
$sha = sha1($originalName.$uniquid);
$resizeWidth = isset($_REQUEST["resize_width"]) ? intval($_REQUEST["resize_width"]) : 0;
$resizeHeight = isset($_REQUEST["resize_height"]) ? intval($_REQUEST["resize_height"]) : 0;
$cropImage = isset($_REQUEST["crop_image"]) ? intval($_REQUEST["crop_image"]) : 0;
// Extract the extension
$originalName = BlobManager::stripInvalidChars($originalName);
$path_info = BlobManager::pathinfo_utf($originalName);
$extension = strtolower($path_info['extension']);
$nameNoExt = $path_info['filename'];
$targetFile = $targetDir . "/" . $sha . "." . $extension;
// Create target dir
if (!file_exists($targetDir))
@mkdir($targetDir, 0777, true);
// Remove old temp files
if (is_dir($targetDir) && ($dir = opendir($targetDir))) {
while (($file = readdir($dir)) !== false) {
$filePath = $targetDir . "/" . $file;
// Remove temp files if they are older than the max age
if (filemtime($filePath) < time() - $maxFileAge)
@unlink($filePath);
}
closedir($dir);
} else
throw new CHttpException (500, Yii::t('app', "Impossibile aprire la directory temporanea."));
// Look for the content type header
if (isset($_SERVER["HTTP_CONTENT_TYPE"]))
$contentType = $_SERVER["HTTP_CONTENT_TYPE"];
if (isset($_SERVER["CONTENT_TYPE"]))
$contentType = $_SERVER["CONTENT_TYPE"];
if (strpos($contentType, "multipart") !== false) {
if (isset($_FILES['file']['tmp_name']) && is_uploaded_file($_FILES['file']['tmp_name'])) {
// Open temp file
$out = fopen($targetFile, $chunk == 0 ? "wb" : "ab");
if ($out) {
// Read binary input stream and append it to temp file
$in = fopen($_FILES['file']['tmp_name'], "rb");
if ($in) {
while ($buff = fread($in, 4096))
fwrite($out, $buff);
} else
throw new CHttpException (500, Yii::t('app', "Impossibile aprire lo stream di input."));
fclose($out);
unlink($_FILES['file']['tmp_name']);
} else
throw new CHttpException (500, Yii::t('app', "Impossibile aprire lo stream di output."));
} else
throw new CHttpException (500, Yii::t('app', "Impossibile spostare il file caricato."));
} else {
// Open temp file
$out = fopen($targetFile, $chunk == 0 ? "wb" : "ab");
if ($out) {
// Read binary input stream and append it to temp file
$in = fopen("php://input", "rb");
if ($in) {
while ($buff = fread($in, 4096))
fwrite($out, $buff);
} else
throw new CHttpException (500, Yii::t('app', "Impossibile aprire lo stream di input."));
fclose($out);
} else
throw new CHttpException (500, Yii::t('app', "Impossibile aprire lo stream di output."));
}
// After last chunk is received, upload file to S3
$ret = array('result' => '1');
if (intval($chunk) + 1 >= intval($chunks)) {
...
}
// Return response
echo CJavaScript::jsonEncode($ret);
Yii::app()->end();
}
protected function disableWebLogRoute()
{
if (true === isset(Yii::app()->log->routes['web']))
Yii::app()->log->routes['web']->enabled = false;
}