Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

option - accept => 'image/*' does not work as expected #106

Open
bologer opened this issue Jun 16, 2017 · 10 comments
Open

option - accept => 'image/*' does not work as expected #106

bologer opened this issue Jun 16, 2017 · 10 comments

Comments

@bologer
Copy link

bologer commented Jun 16, 2017

Have problem with

<?= FileUpload::widget([
    'model' => $uploadModel,
    'attribute' => 'image',
    'url' => ['document/upload'],
    'options' => [
            'accept' => 'image/*'
    ],
    'clientOptions' => [
        'maxFileSize' => 2000000
    ],
...

'accept' => 'image/*' should disallow to upload anything that does not match image/* pattern. Everything seems to be clear, however I am able to upload any type of files (e.g. .zip, .rar, any scrpts).

What could be the cause of this issue?

@bologer bologer changed the title optioms such as accept => 'image/*' do not work as expected option - accept => 'image/*' does not work as expected Jun 16, 2017
@tonydspaniard
Copy link
Member

tonydspaniard commented Jun 16, 2017

Please, try with: https://github.com/blueimp/jQuery-File-Upload/wiki/Options#processqueue

Edit:

Documentation is wrong, on the client level we need to configure: https://github.com/blueimp/jQuery-File-Upload/wiki/Options#acceptfiletypes

@bologer
Copy link
Author

bologer commented Jun 17, 2017

@tonydspaniard, alright, I tried this: acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i as can be seen below:

<?= FileUpload::widget([
    'model' => $uploadModel,
    'attribute' => 'image',
    'url' => ['document/upload'],
   // 'options' => [
   //         'accept' => 'image/*'
   // ],
    'clientOptions' => [
        'acceptFileTypes' => '/(\.|\/)(gif|jpe?g|png)$/i',
        'maxFileSize' => 2000000
    ],
...

However, it did not really help me. I am still able to upload zip archives, etc

I looked at this one - https://github.com/blueimp/jQuery-File-Upload/wiki/Options#processqueue, however no result as well.

Could you please give minimum example of validation?

@bologer
Copy link
Author

bologer commented Jun 19, 2017

@tonydspaniard any suggestions?

@tonydspaniard
Copy link
Member

Sample rules:

public function rules()
    {
        return [
            [['image'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg, jpeg,gif'],
        ];
    }

@tonydspaniard
Copy link
Member

@bologer do you mind pasting the result HTML, the browser you use and check if your console has any javascript errors?

@bologer
Copy link
Author

bologer commented Jun 24, 2017

@tonydspaniard, hey.

Alright, when I use:

'options' => [
    'accept' => 'image/*'
]

I get no errors and any file type is uploaded.

The same story with

'clientOptions' => [
    'acceptFileTypes' => '/(\.|\/)(gif|jpe?g|png)$/i',
    'maxFileSize' => 2000000
],

No errors or anything, files are just being uploaded.

And I noticed that when I try to upload the file that is havier then maxFileSize it reply with 500 (Internal Server Error) on the upload action mentioned in url.

@bologer
Copy link
Author

bologer commented Jun 24, 2017

@tonydspaniard furthermore, when I implemented this in my model, I always receive this error:

array (size=1)
  'image' => 
    array (size=1)
      0 => string 'Please upload a file.' (length=21)

Even though I know for sure that I set image variable when I upload the file.

This is part of action document/upload where I set image variable:

$model = new UploadedFiles();

$model->scenario = UploadedFiles::SCENARIO_UPLOAD;

$imageFile = UploadedFile::getInstance($model, 'image');

And also, this error does not diplay on the client side, it just fails to save() and validate().

I have UploadedFiles model that contains the following rules:

...
return [
            ['image', 'safe'],

            [['image'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg, jpeg, gif'],
...
        ];
...

@tonydspaniard
Copy link
Member

Strange... if you use the FileUpladAction to your controller should return the validation errors: https://github.com/2amigos/yii2-file-upload-widget/blob/master/src/actions/FileUploadAction.php#L152

@tonydspaniard
Copy link
Member

please check this comment: #116 (comment)

@bologer
Copy link
Author

bologer commented Sep 16, 2017

@tonydspaniard I'm sorry, but how do you run this action that you provided? It seems that it is internal of the plugin. Do I need to override it? Could you please show some minimum example of how to use it?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants