Magento 2 Export Product CSV File Permission Issue

 



1 Steps to Solve Magento 2 Export Product CSV File Permission Issue:

2 Conclusion:

Steps to Solve Magento 2 Export Product CSV File Permission Issue:

Step 1: First, you must create a di.xml file in the path below.


app/code/Vendor/Extension/etc/adminhtml/di.xml


And add the following code


<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <preference for="Magento\ImportExport\Controller\Adminhtml\Export\File\Download" type="Vendor\Extension\Controller\Adminhtml\Export\File\Download" />

</config>

Step 2: After that, you must create a preference file in the following path.


app/code/Vendor/Extension/Controller/Adminhtml/Export/File/Download.php


Then add the below-mentioned code snippet


<?php

/**

 * Copyright © Magento, Inc. All rights reserved.

 * See COPYING.txt for license details.

 */

declare(strict_types=1);

 

namespace Vendor\Extension\Controller\Adminhtml\Export\File;

 

use Magento\Backend\App\Action;

use Magento\Framework\App\Action\HttpGetActionInterface;

use Magento\Framework\App\ObjectManager;

use Magento\Framework\App\Response\Http\FileFactory;

use Magento\Framework\App\Filesystem\DirectoryList;

use Magento\ImportExport\Controller\Adminhtml\Export as ExportController;

use Magento\Framework\Filesystem;

use Magento\ImportExport\Model\LocalizedFileName;

use Throwable;

 

/**

 * Controller that download file by name.

 */

class Download extends \Magento\ImportExport\Controller\Adminhtml\Export\File\Download

{

    /**

     * Url to this controller

     */

    const URL = 'adminhtml/export_file/download/';

 

    /**

     * @var FileFactory

     */

    private $fileFactory;

 

    /**

     * @var Filesystem

     */

    private $filesystem;

 

    /**

     * @var LocalizedFileName

     */

    private $localizedFileName;

 

    /**

     * DownloadFile constructor.

     * @param Action\Context $context

     * @param FileFactory $fileFactory

     * @param Filesystem $filesystem

     * @param LocalizedFileName|null $localizedFileName

     */

    public function __construct(

        Action\Context $context,

        FileFactory $fileFactory,

        Filesystem $filesystem,

        ?LocalizedFileName $localizedFileName = null

    ) {

        parent::__construct($context,$fileFactory,$filesystem);

        $this->fileFactory = $fileFactory;

        $this->filesystem = $filesystem;

        $this->localizedFileName = $localizedFileName ?? ObjectManager::getInstance()->get(LocalizedFileName::class);

    }

 

    /**

     * Controller basic method implementation.

     *

     * @return \Magento\Framework\Controller\Result\Redirect | \Magento\Framework\App\ResponseInterface

     */

    public function execute()

    {

 

        $resultRedirect = $this->resultRedirectFactory->create();

        $resultRedirect->setPath('adminhtml/export/index');

        $fileName = $this->getRequest()->getParam('filename');

        

        $exportDirectory = $this->filesystem->getDirectoryRead(DirectoryList::VAR_IMPORT_EXPORT);

 

        try {

           $fileExist = $exportDirectory->isExist('export/' . $fileName);

        } catch (Throwable $e) {

            $fileExist = false;

        }

 

        if (empty($fileName) || !$fileExist) {

            $this->messageManager->addErrorMessage(__('Please provide valid export file name'));

 

            return $resultRedirect;

        }

 

        try {

           

            $path = 'export/' . $fileName;

            $directory = $this->filesystem->getDirectoryRead(DirectoryList::VAR_IMPORT_EXPORT);

            if ($directory->isFile($path)) {

                return $this->fileFactory->create(

                    $fileName,

                    $directory->readFile($path),

                    DirectoryList::VAR_DIR,

                    'application/octet-stream'

                );

 

            }

            $this->messageManager->addErrorMessage(__('%1 is not a valid file', $fileName));

        } catch (\Exception $exception) {

            $this->messageManager->addErrorMessage($exception->getMessage());

        }

 

        return $resultRedirect;

    }

}

Step 3: After that, run the below commands.


php bin/magento setup:di:compile

php bin/magento cache:flush


Comments

Popular posts from this blog

Add Admin User name and Action name in Order Comment Section through Action Performed from Sales Order Grid in Magento 2

How to Update Product Stock Programmatically in Magento 2

How to Set and Get Cookie in Magento 2