0

Here am trying to read the csv file which is uploaded by frontend form. but am not able to open or read the file content. here is form file code.

<form class="import-shipping-form" action="http://website.co/zone_customer/Index/UploadCsv" method="POST" >
  <?php echo $block->getBlockHtml('formkey'); ?>
    <input type="file" required class="required-entry" name="my_custom_file" id="my_custom_file" />
    <button type="submit">Import</button>
</form>

This is the controller

app/code/Zone/Customer/Controller/Index/UploadCsv.php

<?php
namespace Zone\Controller\Index;

class UploadCsv extends \Magento\Framework\App\Action\Action
{
    protected $resultPageFactory;
    protected $jsonHelper;
    protected $_fileUploaderFactory;
    protected $csvProcessor;
public function __construct(
        \Magento\Framework\File\Csv $csvProcessor,
        \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Framework\Json\Helper\Data $jsonHelper
    ) {
        $this->csvProcessor = $csvProcessor;
        $this->_fileUploaderFactory = $fileUploaderFactory;
        $this->resultPageFactory = $resultPageFactory;
        $this->jsonHelper = $jsonHelper;
        parent::__construct($context);
    }
     public function execute()
    {
       $data = $this->getRequest()->getPostValue();
       try{
            if(isset($data['my_custom_file']['0'])) {
            $importProductRawData = $this->csvProcessor->getData($data['my_custom_file']);
             foreach ($importProductRawData as $rowIndex => $dataRow) {
                 print_r($dataRow);
             }
          } catch (\Exception $e) {
            $this->messageManager->addError($e->getMessage());
         }
    }
}

Where am doing wrong. Can I get Help? Thank you in advance.

3
  • can you put the acl.xml file, right away I see your controller is missing HttpPostInterface and ADMIN_RESOURCE. The more of your code we have, the better – Herve Tribouilloy Oct 3 '20 at 9:33
  • Thannk you @Herve Tribouilloy for responding, I dont have acl.xml file in my module. – Praveen Negimani Oct 3 '20 at 9:36
  • is your upload form a frontend or backend form? – Herve Tribouilloy Oct 3 '20 at 12:48
0

You need to use $files = (array) $this->getRequest()->getFiles() I think and not $this->getRequest()->getPostValue();

1
  • Thank you @Gijsve for answering, but its not working. – Praveen Negimani Oct 3 '20 at 9:58
0

Try this code:

 <?php
    namespace Zone\Controller\Index;
    
    class UploadCsv extends \Magento\Backend\App\Action
    {
        protected $resultPageFactory;
        protected $jsonHelper;
        protected $_fileUploaderFactory;
        protected $csvProcessor;
    public function __construct(
            \Magento\Framework\File\Csv $csvProcessor,
            \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,
            \Magento\Framework\App\Action\Context $context,
            \Magento\Framework\View\Result\PageFactory $resultPageFactory,
            \Magento\Framework\Json\Helper\Data $jsonHelper
        ) {
            $this->csvProcessor = $csvProcessor;
            $this->_fileUploaderFactory = $fileUploaderFactory;
            $this->resultPageFactory = $resultPageFactory;
            $this->jsonHelper = $jsonHelper;
            parent::__construct($context);
        }
         public function execute()
        {
           $data = $this->getRequest()->getPostValue();
           try{
                if(isset($data['my_custom_file']['0'])) {
                $tempFile  = $data['my_custom_file']['0']['tmp_name'];
                $importProductRawData = $this->csvProcessor->getData($tempFile);
                 foreach ($importProductRawData as $rowIndex => $dataRow) {
                     print_r($dataRow);
                 }
              } catch (\Exception $e) {
                printr_r($e->getMessage());die;
             }
        }
    }
7
  • my class class UploadCsv extends \Magento\Framework\App\Action\Action. – Praveen Negimani Oct 3 '20 at 10:06
  • Thank you @Arunendra for answer. here am getting exception – Praveen Negimani Oct 3 '20 at 10:07
  • what exception you are getting ? – Arunendra Oct 3 '20 at 10:08
  • actually catch() is added for if block, its fine. but now its blank not printing anything – Praveen Negimani Oct 3 '20 at 10:12
  • See updated answer it will print the error message – Arunendra Oct 3 '20 at 10:24
0

My fault was submit i was using button as submit. and for getting file values am using raw php.

<form class="import-shipping-form" action="<?= $url ?>" method="POST" enctype="multipart/form-data">
        <?php// echo $block->getBlockHtml('formkey'); ?>
        <input type="file" required class="required-entry" name="my_custom_file" id="my_custom_file" />
        <input type='submit' name='submit' value='Upload Products'>
</form>

controller file

try{
     if (isset($_POST['submit'])) 
     {
         $handle = fopen($_FILES['my_custom_file']['tmp_name'], "r");
         $headers = fgetcsv($handle, 1000, ",");
         while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
           {
             echo $data[0];
             echo $data[1];
           }
           fclose($handle);
       }
}catch (\Exception $e) {
    print_r($e->getMessage());
}

now I can see the file content.

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.