5

I am getting an error in admin editor:

Unsupported image format. File: /var/www/clients/mysite/pub/media//Meeting_notes_2__1.docx

My Code is

<type name="Magento\Cms\Model\Wysiwyg\Images\Storage">
        <arguments>
            <argument name="extensions" xsi:type="array">
                    <item name="allowed" xsi:type="array">
                        <item name="pdf" xsi:type="number">1</item>
                        <item name="jpg" xsi:type="number">1</item>
                        <item name="jpeg" xsi:type="number">1</item>
                        <item name="png" xsi:type="number">1</item>
                        <item name="gif" xsi:type="number">1</item>
                        <item name="docx" xsi:type="number">1</item>
                    </item>
                    <item name="image_allowed" xsi:type="array">
                        <item name="jpg" xsi:type="number">1</item>
                        <item name="jpeg" xsi:type="number">1</item>
                        <item name="png" xsi:type="number">1</item>
                        <item name="gif" xsi:type="number">1</item>
                        <item name="docx" xsi:type="number">1</item>
                    </item>
                    <item name="media_allowed" xsi:type="array">
                        <item name="flv" xsi:type="number">1</item>
                        <item name="swf" xsi:type="number">1</item>
                        <item name="avi" xsi:type="number">1</item>
                        <item name="mov" xsi:type="number">1</item>
                        <item name="rm" xsi:type="number">1</item>
                        <item name="wmv" xsi:type="number">1</item>
                        <item name="docx" xsi:type="number">1</item>
                    </item>
            </argument>
        </arguments>
    </type>
    <preference for="Magento\Cms\Model\Wysiwyg\Images\Storage" type="Vendor\ModuleName\Model\Cms\Wysiwyg\Images\Storage" />
6
  • You get this error because of you have defined Type for Image (Magento\Cms\Model\Wysiwyg\Images\Storage) and pass argument item for docx You can see in magento core file/vendor/magento/module-cms/etc/di.xml there is no such a file type extension for you given Type. Jun 24, 2019 at 11:36
  • Using the above code what do you want to achieve? what do you want to do? Jun 24, 2019 at 11:37
  • @chirag I have updated my code. Can you please check and suggest the solution for this
    – Shomita
    Jun 24, 2019 at 12:40
  • Why you are pass <item name="docx" xsi:type="number">1</item> what do you want? do you want file upload functionality in admin or frontend form? Jun 24, 2019 at 13:13
  • @ChiragPatel I need upload funtionality for Wysiwyg editor in magento2 admin.
    – Shomita
    Jun 25, 2019 at 4:30

2 Answers 2

1

There is no need to pass docx file extension to <item name="image_allowed" xsi:type="array"> and <item name="media_allowed" xsi:type="array"> try with below way it's working for me in Magento 2.3.1.

app/code/[Company]/[Module]/etc/di.xml

Add below code.

<type name="Magento\Cms\Model\Wysiwyg\Images\Storage">
        <arguments>
            <argument name="extensions" xsi:type="array">
                <item name="allowed" xsi:type="array">
                    <item name="docx" xsi:type="number">1</item>
                    <item name="pdf" xsi:type="number">1</item>
                </item>
            </argument>
        </arguments>
    </type>
    <preference for="Magento\Cms\Model\Wysiwyg\Images\Storage" type="[Company]\[Module]\Model\Cms\Wysiwyg\Images\Storage" />

create Storage.php file at below path.

app/code/[Company]/[Module]/Model/Cms/Wysiwyg/Images/Storage.php

    <?php

namespace [Company]\[Module]\Model\Cms\Wysiwyg\Images;

class Storage extends \Magento\Cms\Model\Wysiwyg\Images\Storage
{
    public function uploadFile($targetPath, $type = null)
    {
        /** @var \Magento\MediaStorage\Model\File\Uploader $uploader */
        $uploader = $this->_uploaderFactory->create(['fileId' => 'image']);
        $allowed = $this->getAllowedExtensions($type);
        if ($allowed) {
            $uploader->setAllowedExtensions($allowed);
        }
        $uploader->setAllowRenameFiles(true);
        $uploader->setFilesDispersion(false);
        $result = $uploader->save($targetPath);

        if (!$result) {
            throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t upload the file right now.'));
        }

        // Change Start
        if (strtolower($uploader->getFileExtension()) !== 'docx' && strtolower($uploader->getFileExtension()) !== 'pdf') {
            // Create Thumbnail
            $this->resizeFile($targetPath . '/' . $uploader->getUploadedFileName(), true);
        }

        $result['cookie'] = [
            'name' => $this->getSession()->getName(),
            'value' => $this->getSession()->getSessionId(),
            'lifetime' => $this->getSession()->getCookieLifetime(),
            'path' => $this->getSession()->getCookiePath(),
            'domain' => $this->getSession()->getCookieDomain(),
        ];

        return $result;
    }
}

Run php bin/magento cache:clean

Then Go to Admin > Content > Page > Edit or Add new page Inside content click on insert image and upload docx file

I hope it helps!

7
  • I did already same for Pdf. This is working for pdf file but need to enable also for doc and docx file in editor
    – Shomita
    Jun 25, 2019 at 5:08
  • Just copy and paste my code, I have just test now it's working. And let me know. Jun 25, 2019 at 5:11
  • Okay let me check and update you
    – Shomita
    Jun 25, 2019 at 5:17
  • 1
    Your code is working fine. you need to update one thing. If you are adding this line ---- <item name="pdf" xsi:type="number">1</item>-- for pdf then you need to add pdf in this condition if (strtolower($uploader->getFileExtension()) !== 'pdf') .. Please update your comment. ------------------>Also add for docx file with this way <item name="docx" xsi:type="number">1</item> and add condition like this if (strtolower($uploader->getFileExtension()) !== 'docx' && strtolower($uploader->getFileExtension()) !== 'pdf')
    – Shomita
    Jun 25, 2019 at 6:02
  • Ohhh, sorry it is a typo mistake. I have updated my answer. see and you can accept the answer so it will usefull for other users :) Thanks. Jun 25, 2019 at 6:14
2

Please Look at here,

Please change this file

project/vendor/magento/module-cms/etc/di.xml

Add any extension you want to add <item name="docx" xsi:type="number">1</item>

<argument name="extensions" xsi:type="array">
<item name="allowed" xsi:type="array">
    <item name="jpg" xsi:type="number">1</item>
    <item name="jpeg" xsi:type="number">1</item>
    <item name="png" xsi:type="number">1</item>
    <item name="gif" xsi:type="number">1</item>
    <item name="docx" xsi:type="number">1</item>
</item>
<item name="image_allowed" xsi:type="array">
    <item name="jpg" xsi:type="number">1</item>
    <item name="jpeg" xsi:type="number">1</item>
    <item name="png" xsi:type="number">1</item>
    <item name="gif" xsi:type="number">1</item>
    <item name="docx" xsi:type="number">1</item>
</item>
<item name="media_allowed" xsi:type="array">
    <item name="flv" xsi:type="number">1</item>
    <item name="swf" xsi:type="number">1</item>
    <item name="avi" xsi:type="number">1</item>
    <item name="mov" xsi:type="number">1</item>
    <item name="rm" xsi:type="number">1</item>
    <item name="wmv" xsi:type="number">1</item>
    <item name="docx" xsi:type="number">1</item>
</item>

9
  • This is not working.. already tried it
    – Shomita
    Jun 24, 2019 at 11:00
  • Check my updated code. Jun 24, 2019 at 11:01
  • what you have updated in this code?
    – Shomita
    Jun 24, 2019 at 11:03
  • In second add <item name="image_allowed" xsi:type="array"> add this line :- <item name="docx" xsi:type="number">1</item> Jun 24, 2019 at 11:03
  • This is adding doc and docx file but showing error. I am not able to figure out how to fix this.
    – Shomita
    Jun 24, 2019 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.