2amigos / yii2-file-upload-widget

BlueImp File Upload Widget for Yii2
Other
252 stars 140 forks source link

SyntaxError: Unexpected token < in JSON at position 0 #155

Open newscloud opened 5 years ago

newscloud commented 5 years ago

I'm also getting same error as earlier person. Reopening new issue.

SyntaxError: Unexpected token < in JSON at position 0

I've been working on this several hours. I agree with others. While we appreciate you offering a plugin, the examples are not sufficient.

tonydspaniard commented 5 years ago

@newscloud can I see your code, implementation? thanks

newscloud commented 5 years ago
public function actionCreate()
    {
        $model = new Photo();
        $model->id=1;
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['create', 'id' => $model->id]);
        }
        return $this->render('create', [
            'model' => $model,
        ]);
    }

     public function actionUpload()
{
  $model = new Photo();
  $model->id=2;
  $model->image = UploadedFile::getInstance($model, 'image');
    if ($model->image !== null && $model->validate(['image'])) {
        Yii::$app->response->getHeaders()->set('Vary', 'Accept');
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $response = [];

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

<div class="photo-create">

    <h1><?= Html::encode($this->title) ?></h1>

    <?= FileUploadUI::widget([
        'model' => $model,
        'attribute' => 'user_id',
/* I think I also had tried photo/upload */
        'url' => ['photo/create', 'id' => $model->id],
        'gallery' => false,
        'fieldOptions' => [
            'accept' => 'image/*'
        ],
        'clientOptions' => [
            'maxFileSize' => 2000000
        ],
        // ...
        'clientEvents' => [
            'fileuploaddone' => 'function(e, data) {
                                    console.log(e);
                                    console.log(data);
                                }',
            'fileuploadfail' => 'function(e, data) {
                                    console.log(e);
                                    console.log(data);
                                }',
        ],
    ]); ?>
    <?php
    /* = $this->render('_form', [
        'model' => $model,
    ]) */?>
elmys commented 5 years ago

@newscloud check for rightly line 'url' => ['photo/create', 'id' => $model->id],

tonydspaniard commented 5 years ago

@newscloud it looks ok as you use the same strategy I used on the upload action: https://github.com/2amigos/yii2-file-upload-widget/blob/master/src/actions/FileUploadAction.php#L104

This should be 'url' => ['photo/upload', 'id' => $model->id] as the create renders HTML and not JSON.

Other than the wrong action name, the rest looks fine