Yii Framework Forum: Yii-Booster Redactor Image Upload Problem. - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Yii-Booster Redactor Image Upload Problem. Almost works.. Rate Topic: -----

#1 User is offline   JCJ 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 19-November 12

  Posted 05 March 2013 - 09:08 AM

I'm trying to implement file uploads using the redactor editor bundled with Yii-booster. It's a current version of redactor (Dec 2012 I believe).

The image uploads *work*, but after you select the file to upload the file is transferred and stored but does not show in the window to insert it into the editor area.

If you click cancel on the upload window then re-open it, the file I uploaded shows up in the list of files on the server and can be inserted.

EDIT: Got it working, here's the controller code for those interested.



View:
<?php echo $form->redactorRow($model,'descr', array('options' => array('imageUpload' => Yii::app()->createAbsoluteUrl('admin/event/imageUpload'),
                                                                                'imageGetJson' => Yii::app()->createAbsoluteUrl('admin/event/listImages')))); ?>[code]


[code]

    public function actionImageUpload() {
        $uploadedFile = CUploadedFile::getInstanceByName('file');
        if (!empty($uploadedFile)) {
            $rnd = rand();  // generate random number between 0-9999
            $fileName = "{$rnd}.{$uploadedFile->extensionName}";  // random number + file name
            if ($uploadedFile->saveAs(Yii::app()->basePath . '/../images/' . $fileName)) {
                
                $array = array(
                     'filelink' => Yii::app()->baseUrl . '/images/' . $fileName);
               // echo CHtml::image(Yii::app()->baseUrl . '/images/' . $fileName);
                
                echo stripslashes(json_encode($array));
                Yii::app()->end();
            }
        }
        throw new CHttpException(400, 'The request cannot be fulfilled due to bad syntax');
    }

// "ListImages" (used to browse images in the server)

    public function actionListImages() {

        $images = array();
        $handler = opendir(Yii::app()->basePath . '/../images');
        while ($file = readdir($handler)) {
            if ($file != "." && $file != "..")
                $images[] = $file;
        }
        closedir($handler);

        $jsonArray = array();

        foreach ($images as $image)
            $jsonArray[] = array(
                'thumb' => Yii::app()->baseUrl . '/images/' . $image,
                'image' => Yii::app()->baseUrl . '/images/' . $image,
            );

        header('Content-type: application/json');
        echo CJSON::encode($jsonArray);
    }






0

#2 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 05 March 2013 - 09:43 AM

/* moved from General Discussion */
"Less noise - more signal"
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users