Berdasarkan jawaban yang Marius berikan.
class Download extends \Magento\Framework\App\Action\Action
{
protected $resultRawFactory;
protected $fileFactory;
public function __construct(
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
\Magento\Framework\App\Response\Http\FileFactory $fileFactory,
\Magento\Backend\App\Action\Context $context
) {
$this->resultRawFactory = $resultRawFactory;
$this->fileFactory = $fileFactory;
parent::__construct($context);
}
public function execute()
{
try{
$fileName = 'FileName'; // the name of the downloaded resource
$this->fileFactory->create(
$fileName,
[
'type' => 'filename',
'value' => 'relative/path/to/file/from/basedir'
],
DirectoryList::MEDIA , //basedir
'application/octet-stream',
'' // content length will be dynamically calculated
);
}catch (\Exception $exception){
// Add your own failure logic here
var_dump($exception->getMessage());
exit;
}
$resultRaw = $this->resultRawFactory->create();
return $resultRaw;
}
}
Tidak memiliki izin yang benar (meskipun membaca diperlukan di sini, Magento memeriksa izin menulis) akan menghasilkan kesalahan aneh. "Situs ini down atau pindah" atau bertiga seperti itu.
Layak mengambil puncak menyelinap di logika di dalam $ fileFactory-> create () juga.