2

I have implemented multiple image upload using the following code.

   <field name="press_images">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="dataType" xsi:type="string">string</item>
                <item name="source" xsi:type="string">press_images</item>
                <item name="label" xsi:type="string" translate="true">Image</item>
                <item name="visible" xsi:type="boolean">true</item>
                <item name="formElement" xsi:type="string">imageUploader</item>
                <item name="elementTmpl" xsi:type="string">ui/form/element/uploader/uploader</item>
                <item name="previewTmpl" xsi:type="string">Magento_Catalog/image-preview</item>
                <item name="isMultipleFiles" xsi:type="boolean">true</item>
                <item name="validation" xsi:type="array">
                    <item name="required-entry" xsi:type="boolean">true</item>
                </item>
                <item name="uploaderConfig" xsi:type="array">
                    <item name="url" xsi:type="url" path="pressfeature/image/upload"/>
                </item>
            </item>
        </argument>
    </field>

I used https://github.com/Turiknox/magento2-sample-imageuploader way to upload images. The image uploading works fine. When you browser the image and choose it. Then a controller gets fired which uploads all images in temp folder using an ajax call. When i click save button the image data and other data is send correctly.

My question is how should i save this filename in database so when i click edit i should see both images in edit form.

5
  • 1
    This is custom image? where you want to save it? in custom table? Commented May 16, 2019 at 5:24
  • Only the name of the file but the file will be moved from tmp directory to <module media> directory.
    – Avesh Naik
    Commented May 16, 2019 at 5:30
  • 1
    But where you want to insert filename in DB? Commented May 16, 2019 at 5:31
  • Yes in database.
    – Avesh Naik
    Commented May 16, 2019 at 5:32
  • Thank you for the help. Got the solution.
    – Avesh Naik
    Commented May 16, 2019 at 5:36

1 Answer 1

1

Got the solution, Changes need to dataProvider file.

public function getData()
{
    if (isset($this->_loadedData)) {
        return $this->_loadedData;
    }
    $items = $this->collection->getItems();
    foreach ($items as $item) {
        if ($item->getDefaultImage()) {
            $img['default_image'][0]['name'] = $item->getDefaultImage();
            $img['default_image'][0]['url'] = $this->getMediaUrl()."/".$item->getDefaultImage();
            $fullData = $item->getData();
            $this->_loadedData[$item->getId()] = array_merge($fullData, $img);
        }
        else {
            $this->_loadedData[$item->getId()] = $item->getData();
        }
    }
    return $this->_loadedData;
}

FROM THIS

$img['default_image'][0]['name'] = $item->getDefaultImage();
$img['default_image'][0]['url'] = $this->getMediaUrl()."/".$item->getDefaultImage();

TO THIS

$img['default_image'][$i]['name'] = $item->getDefaultImage();
$img['default_image'][$i]['url'] = $this->getMediaUrl()."/".$item->getDefaultImage();

where $i is the number of image uploaded. save all file names in an array in database.

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.