Try this way, it will help you.
In your controller file that $postUrl
points to:
protected $csv;
public function __construct(
\Magento\Framework\File\Csv $csv
)
{
$this->csv = $csv;
}
//The function name should match your controller path
public function import($file)
{
if (!isset($file['tmp_name']))
throw new \Magento\Framework\Exception\LocalizedException(__('Invalid file upload attempt.'));
$csvData = $this->csv->getData($file['tmp_name']);
foreach ($csvData as $row => $data) {
if ($row > 0){
//Start your work
}
}
die();
}
Note:
- The
$data
is an array object already. It will look like array(
0 => 'UPS',
1 => 'One Night Service'
......
)
- I added
if ($row > 0)
to skip first row as it's the attribute row only.
- You must
die()
the execution after your job done.
I assume your controller is custom made. If you want to override the core controller.
For more reference Click here