Git Product home page Git Product logo

Comments (11)

gimox avatar gimox commented on June 3, 2024

error
for more precision the error is:

from yii2-file-upload-widget.

gimox avatar gimox commented on June 3, 2024

ok so i read that jquery file upload need a specific json respons. i implemented it.
Can you add an example with your yii2 controller code

from yii2-file-upload-widget.

fabiomlferreira avatar fabiomlferreira commented on June 3, 2024

Can you show us what is the json response that your controller send?

you need something similar to this https://github.com/blueimp/jQuery-File-Upload/wiki/JSON-Response

In your controller you need tho receive the file and save it:
$file = \yii\web\UploadedFile::getInstance($model, 'attribute');
$file->saveAs($imagepath.$file->name);

then create an json string with the field that you need and return it without anything else, only the json string.

I already try this plugin but it's I change to this http://demos.krajee.com/widget-details/fileinput, since I need multiple input fields per form and with yii2-file-upload-widget it's a mess

from yii2-file-upload-widget.

tonydspaniard avatar tonydspaniard commented on June 3, 2024

@gimox @invaderhd Sorry you got me on holidays. As soon as I am back will write that info.
This is an example of the controller action handling file upload (hope it makes sense to you guys, otherwise you will have to wait till i am back):

public function actionUpload($id)
    {
        $tour = Tour::findOne($id);
        if (!$tour) {
            throw new NotFoundHttpException(Yii::t('app', 'Page not found'));
        }
        $picture = new TourPicture(['scenario' => 'upload']);
        $picture->tour_id = $id;
        $picture->image = UploadedFile::getInstance($picture, 'image');
        if ($picture->image !== null && $picture->validate(['image'])) {

            Yii::$app->response->getHeaders()->set('Vary', 'Accept');
            Yii::$app->response->format = Response::FORMAT_JSON;

            $response = [];

            if ($picture->save(false)) {
                $response['files'][] = [
                    'name' => $picture->image->name,
                    'type' => $picture->image->type,
                    'size' => $picture->image->size,
                    'url' => $picture->getImageUrl(),
                    'thumbnailUrl' => $picture->getImageUrl(TourPicture::SMALL_IMAGE),
                    'deleteUrl' => Url::to(['delete', 'id' => $picture->id]),
                    'deleteType' => 'POST'
                ];
            } else {
                $response[] = ['error' => Yii::t('app', 'Unable to save picture')];
            }
            @unlink($picture->image->tempName);
        } else {
            if ($picture->hasErrors(['picture'])) {
                $response[] = ['error' => HtmlHelper::errors($picture)];
            } else {
                throw new HttpException(500, Yii::t('app', 'Could not upload file.'));
            }
        }
        return $response;
    }

from yii2-file-upload-widget.

gimox avatar gimox commented on June 3, 2024

ok. thanks.
The problem is not your widget but the jQuery file upload that i never used before.
So it has a specific response.
i add a json response in the correct format and it work.
So it's very confortable the option that you have added for changing template in form,upload and template. I change it for a speedy custom upload widget.
Good work Tony!

from yii2-file-upload-widget.

tonydspaniard avatar tonydspaniard commented on June 3, 2024

@gimox thanks... sorry for the delay but is due to vacation

from yii2-file-upload-widget.

gimox avatar gimox commented on June 3, 2024

No problem.

from yii2-file-upload-widget.

konjevicante avatar konjevicante commented on June 3, 2024

Is it possible to get a snippet for this widget in case of editing? Lets say you've already attached some photos to a specific model and now you want to remove some and add new?

P.S. Thanks Tony! Great work, really helped me!

from yii2-file-upload-widget.

konjevicante avatar konjevicante commented on June 3, 2024

Solved with

View

$script = "jQuery.ajax({
                url: jQuery('#photos-image-form').fileupload('option', 'url') + '&read=1',
                dataType: 'json',
                context: jQuery('#photos-image-form')[0]
            }).done(function (result) {
            console.log(result);
                jQuery(this).fileupload('option', 'done').call(this, jQuery.Event('done'), {result: result});
            });";

$this->registerJs($script); 

Controller, url with read:

        if($read){
            $response = [];
            $array = Model::find()->where(['item_id' => $id])->all();

            foreach($array as $photo){
                $response['files'][] = [
                        'name' => $photo->title,
                        'type' => 'jpeg',
                        'size' => '123',
                        'url' => $photo->getFile(),
                        'thumbnailUrl' => $photo->getThumbnail(),
                        'deleteUrl' => Url::to(['delete-image', 'id' => $photo->id]),
                        'deleteType' => 'POST'
                ];
            }

            return json_encode($response);
        }

And after deleting via delete url

Return value in delete:

 return $this->actionUpload($read_id, 1);

from yii2-file-upload-widget.

tonydspaniard avatar tonydspaniard commented on June 3, 2024

@konjevicante Thanks for posting the solution here

from yii2-file-upload-widget.

cyb19920 avatar cyb19920 commented on June 3, 2024

the view how get callback

from yii2-file-upload-widget.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.