How to get PDF file URL uploaded in system.xml. Magento 2
-
14-04-2021 - |
Pergunta
I'm trying to upload PDF file via system.xml
my code is
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="infozzle" translate="label" sortOrder="11">
<label>Infozzle Extension</label>
</tab>
<section id="infozzle_productpagecustom" translate="label" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Product PDF File</label>
<tab>infozzle</tab>
<resource>Infozzle_ProductPageCustom::config_productpagecustom</resource>
<group id="general" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>General Setting</label>
<field id="custom_file_upload" translate="label" type="Magento\Config\Block\System\Config\Form\Field\File" sortOrder="6" showInDefault="1" showInWebsite="1" >
<label>PDF File Upload</label>
<upload_dir config="system" scope_info="1">pdf</upload_dir>
</field>
</group>
</section>
</system>
</config>
n my Helper Function is
public function getPDFfile()
{
$getarray = $this->scopeConfig->getValue('infozzle_productpagecustom/general/custom_file_upload',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
return $getarray;
}
so when I get the file using Helper in phtml like
echo $helper->getPDFfile();
then it returns me
Asad's Resume.pdf,application/pdf,/tmp/phpDGlEAI,0,65105
While When I tried to Delete the existing file it is not deleting n adding more files
how can I get the exact url
of the file n why it returns such ambiguous result
Solução
The Problem has been sorted we need to add <backend_model>Magento\Config\Model\Config\Backend\File</backend_model>
in field
like
<field id="custom_file_upload" translate="label" type="Magento\Config\Block\System\Config\Form\Field\File" sortOrder="6" showInDefault="1" showInWebsite="1" >
<label>PDF File Upload</label>
<backend_model>Magento\Config\Model\Config\Backend\File</backend_model>
<upload_dir config="system" scope_info="1">pdf</upload_dir>
</field>
and after that we need to add class
Vender\Module\Model\Config\Backend\CustomFileType
<?php
namespace Vender\Module\Model\Config\Backend;
class CustomFileType extends \Magento\Config\Model\Config\Backend\File
{
/**
* @return string[]
*/
public function getAllowedExtensions() {
return ['pdf'];
}
}
in my case I want PDF thats why I return pdf
you can change it to csv
or anything else
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange