cancel
Showing results for 
Search instead for 
Did you mean: 

How can I have an admin controller in Magento and use it in phhtml for uploading file

How can I have an admin controller in Magento and use it in phhtml for uploading file

 
I'm beginner in PHP and Magento, I have Plugin which has a admin menu which refers to a page. What I want to do is adding an admin controller for uploading a file to server from the page. this is my structure:
  • MyPlugin/KnownUser/Controller/Adminhtml/Admin/Index.php

    <?phpnamespace MyPlugin\KnownUser\Controller\Adminhtml\Admin;require_once( __DIR__ .'../../../../IntegrationInfoProvider.php');
    use \DateTime;
    
      class Index extends \Magento\Backend\App\Action
      {
        /**
        * @var \Magento\Framework\View\Result\PageFactory
        */
        protected $resultPageFactory;
        public function __construct(     \Magento\Backend\App\Action\Context $context,     \Magento\Framework\View\Result\PageFactory $resultPageFactory) 
    {     parent::__construct($context);     $this->resultPageFactory = $resultPageFactory;
    
    }
    
    public function execute()
    {           
        $configProvider = new \MyPlugin\KnownUser\IntegrationInfoProvider();    $configText =  $configProvider->getIntegrationInfo(true);    $customerIntegration = json_decode($configText, true);    $resultPage = $this->resultPageFactory->create();    $layout = $resultPage->getLayout();    $block = $layout->getBlock('main_panel');    $block->setAccountId($customerIntegration["AccountId"]);    $block->setVersion($customerIntegration["Version"]);    $block->setPublishDate($customerIntegration["PublishDate"]);    $block->setUploadUrl($this->getUrl('knownuser/admin/process/index'));   
        $block->setIntegrationConfig( $configText);
        return $resultPage;
    
    }   
    
    }
    • MyPlugin\knownuser\view\adminhtml\templates\admin.phtml

 

      <p>
          <label style="width:200px">Publisher</label>
          <br />          <input readonly type="text" value="<?php echo $this->getAccountId() ?>"> </input>
      </p>
      <p>
          <label style="width:100px">Version</label>
          <br />          <input readonly type="text" value="<?php echo $this->getVersion() ?>">
          </input>
      </p>
      <p>
          <label style="width:100px">Publish Date</label>
          <br />          <input readonly type="text" value="<?php echo $this->getPublishDate() ?
          >" > </input>
      </p>
      <p>
          <label style="width:100px">IntegrationdedConfig</label>
      </p>
      
      <textarea rows="10" cols="200" readonly>
      <?php echo $this->getIntegrationConfig() ?>
            </textarea>
      
      <form id='form' method="post" enctype="multipart/form-data">
          <input type="file" name="files[]" multiple>
          <input type="submit" value="Upload File" name="submit">
      </form>
      <script>
      
          const url = '<?php echo $this->getUploadUrl()?>';          alert(url);
          const form = document.getElementById('form');      
          form.addEventListener('submit', e => {              e.preventDefault();
              const files = document.querySelector('[type=file]').files;
              const formData = new FormData();
      
              for (let i = 0; i < files.length; i++) {
                  let file = files[i];      
                  formData.append('files[]', file);
              }
              debugger;              fetch(url, {                  method: 'POST',                  body: formData
              }).then(response => {                  console.log(response);
              });
          });
      </script>

 

So what I want to do is add a new controller and post my file to server, but I don't know how can I do that. I tried to add another controller and post my file from JavaScript but it didn't work and just redirect my request. my controller is like this:

  • MyPlugin\knownuser\Controller\Adminhtml\Process

      <?php
      namespace MyPlugin\KnownUser\Controller\Adminhtml\Admin;
      class Index extends \Magento\Framework\App\Action\Action
      {
         public function execute()
        {           
    
           die('test index');       echo('just for sure it has been ran');
        }   
    
      }