TransWikia.com

Magento 2.4: How to display the filename after uploading the file via ajax?

Magento Asked by Siva on January 28, 2021

I want to show the filename instead of uniqid(). I have the below code in my controller, how can I display the actual file name after uploading the file

<?php

namespace VendorModuleControllerIndex;

use MagentoFrameworkJsonHelperData as JsonHelper;

class Upload extends MagentoFrameworkAppActionAction {

    protected $_mediaDirectory;
    protected $_fileUploaderFactory;
    public $_storeManager;

    public function __construct(
        MagentoBackendAppActionContext $context,
        JsonHelper $jsonHelper,
        MagentoFrameworkControllerResultJsonFactory $resultJsonFactory,
        MagentoFrameworkFilesystem $filesystem,
        MagentoMediaStorageModelFileUploaderFactory $fileUploaderFactory,
        MagentoStoreModelStoreManagerInterface $storeManager
    ) {
        parent::__construct($context);
        $this->jsonHelper = $jsonHelper;
        $this->resultJsonFactory = $resultJsonFactory;
        $this->_mediaDirectory = $filesystem->getDirectoryWrite(MagentoFrameworkAppFilesystemDirectoryList::MEDIA);
        $this->_fileUploaderFactory = $fileUploaderFactory;
        $this->_storeManager = $storeManager;
    }


    public function execute(){

        $_postData = $this->getRequest()->getPost();

        $message = "";
        $newFileName = "";
        $error = false;
        $data = array();

        try{
            $target = $this->_mediaDirectory->getAbsolutePath('module/tmp/attachment/');

            //attachment is the input file name posted from your form
            $uploader = $this->_fileUploaderFactory->create(['fileId' => 'attachment_upload']);

            $_fileType = $uploader->getFileExtension();
            $newFileName = uniqid().'.'.$_fileType;

            /** Allowed extension types */
            $uploader->setAllowedExtensions(['pdf', 'jpeg', 'png', 'jpg']);
            /** rename file name if already exists */
            $uploader->setAllowRenameFiles(true);

            //$result = $uploader->save($target, $newFileName); //Use this if you want to change your file name
            $result = $uploader->save($target, $newFileName); //Use this if you want to change your file name
            //$result = $uploader->save($target);
            if ($result['file']) {
                $_mediaUrl = $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA);
                $_iconArray = array(
                    'pdf' => $_mediaUrl.'module/default/icon-pdf.png',
                    'jpeg' => $_mediaUrl.'module/default/icon-jpeg.png',
                    'png' => $_mediaUrl.'module/default/icon-png.png',
                    'jpg' => $_mediaUrl.'module/default/icon-jpg.png',
                );

                if(isset($_iconArray[$_fileType])){
                    $_src = $_iconArray[$_fileType];
                }else{
                    $_src = $_mediaUrl.'module/tmp/attachment'.$attachment;
                }
                $error = false;
                $message = "File has been successfully uploaded";

                $html = '<div class="image item base-image" data-role="image" id="'. uniqid().'">
                            <div class="product-image-wrapper">
                                <img class="product-image" data-role="image-element" src="'.$_src.'" alt="">
                                <div class="actions">
                                    <button type="button" class="action-remove" data-role="delete-button" data-image="'.$newFileName.'" title="Delete image"><span>Delete image</span></button>
                                </div>
                                <div class="image-fade"><span>Hidden</span></div>
                            </div>
                            <div class="item-description">
                                <div class="item-title" data-role="img-title"></div>
                                <div class="item-size">
                                    <a href="'.$_mediaUrl.'module/tmp/attachment/'.$newFileName.'" target="_blank"><span data-role="image-dimens">'.$newFileName.'</span></a>
                                </div>
                            </div>
                        </div>';

                $data = array(
                    'filename' => $newFileName,
                    'path' => $_mediaUrl.'module/tmp/attachment/'.$newFileName,
                    'fileType' => $_fileType,
                    'html' => $html
                );
            }
        } catch (Exception $e) {
            $error = true;
            $message = $e->getMessage();
        }

        $resultJson = $this->resultJsonFactory->create();

        return $resultJson->setData([
                    'message' => $message,
                    'data' => $data,
                    'error' => $error
        ]);
    }
}

Any help will be appreciated

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP