4

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" />
  • 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. – Chirag Patel Jun 24 at 11:36
  • Using the above code what do you want to achieve? what do you want to do? – Chirag Patel Jun 24 at 11:37
  • @chirag I have updated my code. Can you please check and suggest the solution for this – Shiwani Jun 24 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? – Chirag Patel Jun 24 at 13:13
  • @ChiragPatel I need upload funtionality for Wysiwyg editor in magento2 admin. – Shiwani Jun 25 at 4:30
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!

  • I did already same for Pdf. This is working for pdf file but need to enable also for doc and docx file in editor – Shiwani Jun 25 at 5:08
  • Just copy and paste my code, I have just test now it's working. And let me know. – Chirag Patel Jun 25 at 5:11
  • Okay let me check and update you – Shiwani Jun 25 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') – Shiwani Jun 25 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. – Chirag Patel Jun 25 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>

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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