0

I have override following files for

  • Displaying File Upload Fields - Magento_Sales::order/view/history.phtml
  • To Process Comments -Magento\Sales\Controller\Adminhtml\Order\AddComment
  • To Display Block - Magento\Sales\Block\Adminhtml\Order\View\History

enter image description here

When i change <input name="history[file]" type="file" to <input name="history[attachment]" type="text" it works. Input text value can be received at controller level. Looks like file input is not processed in controller.

  • Have to create any database field to save this file path? – Amit Bera Aug 27 '18 at 15:19
  • Yes, In controller Vendor\Module\Controller\Adminhtml\Order\AddComment when i var_dump() post parameter then name of file field does display. In Short if input type is file then in controller input name does not process. If input type is text then this field is processed in controller. – Aniruddha A Deshpande Aug 27 '18 at 15:44
0

In Order, the comment is saving on Ajax request. So for this, It will not save file just using input type="file". You need to:

  1. Modify Flow Remove entire flow from ajax based to form submit based and add enctype: multipart/form-data.

To Process Comments - Magento\Sales\Controller\Adminhtml\Order\AddComment print_r( $_FILES); // you will get all details about file for upload. And add custom changes to save file and comment.

2.With Current Flow ( Ajax Based ) As Magento2 Is rich with UI components feature. You can also use upload UI components feature.

<div class="upload-wrapper" data-bind="scope: 'uploader'">
    </div> <script type="text/x-magento-init">
    {
       ".upload-wrapper": {
           "Magento_Ui/js/core/app": {
               "components": {
                   "uploader": {
                       "component": "Magento_Ui/js/form/element/file-uploader",
                       "template": "ui/form/element/uploader/uploader",
                       "allowedExtensions": "png",
                       "uploaderConfig": {
                            "url": "<?php echo $this->getUrl('sales/*/addComment', ['order_id' => $block->getOrder()->getId(),'type'=>'upload']) ?>"
                       }
                   }
               }
           }
       }
    } </script>

Do some changes to support file upload functionality in sales/*/addComment . Or Create Separate Controller for upload file functionality.

public function execute()
    {

    $error = false;

    // Do something with the file post var_dump($_FILES['files'])

    $result = ['error'=>$error,'success'=> ($error) ? false : true ];

    try {
            return $this->jsonResponse($result);
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            return $this->jsonResponse($e->getMessage());
        } catch (\Exception $e) {
            $this->logger->critical($e);
            return $this->jsonResponse($e->getMessage());
        }
    }
  1. Get File Path in response and append it in any hidden input to save file path in the comment.
  2. And Submit Comment on addComment Action you need to save file path which is in the hidden field.

Hope this works for you.

Good Luck!

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.