Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/sbboke.com/wp-content/plugins/wordpress-seo/src/integrations/front-end-integration.php:409) in /www/wwwroot/sbboke.com/wp-content/themes/pacify/content-single.php on line 5

Magento 2系统配置中文件上传

使用Magento 2管理界面时,可以完全在Magento 2系统配置中上传文件。在Magento 2中,管理界面接受各种输入类型,如文本文件,单选按钮,下拉列表以及普通,加密或序列化的多个选择,然后以不同的方式显示,如网格,表单,简单字段和图像。特别是,Magento还允许您扩展默认界面,然后您可以自由添加字段。现在让我们阅读这篇文章并获得详细的说明。

在Magento 2商店配置中上传文件的概述

  • 第1步:创建system.xml文件
  • 第2步:刷新缓存并检查结果

第1步:创建system.xml文件。

按照路径Store > Configuration,在Sales部分下,您可以创建新的部分custom_section,创建一组新的字段custom_group,并custom_file_upload通过system.xml在模块中添加文件来创建文件上载

应用以下代码段。文件路径:app/code/Mageplaza/HelloWorld/etc/adminhtml/system.xml

<section id="mageplaza_helloworld_section" translate="label" type="text" sortOrder="301" showInDefault="1" showInWebsite="1" showInStore="0">
    <label>Sales</label>
    <tab>sales</tab>
    <resource>Magento_Sales::config_sales</resource>
    <group id="custom_group" translate="label" type="text" sortOrder="6" showInDefault="1" showInWebsite="1" >
        <label>Custom group</label>
        <field id="custom_file_upload" translate="label" type="Magento\Config\Block\System\Config\Form\Field\File" sortOrder="6" showInDefault="1" showInWebsite="1" >
            <label>Upload custom file</label>
        </field>
    </group>
</section>

在上面的脚本中,有一些要点需要知道它们是什么。首先,Section字段是不言自明的,而tab是该部分的确切位置,sales设置为Magento_Sales::etc/adminhtml/system.xml file,并且资源将应用于ACL。但是,请记住只有Magento_Sales::config_sales可能访问此部分的管理员。

接下来,Group在脚本代码中,require将包含允许您根据需要上载文件的字段。该组包含idtype属性。id指向某个自定义文件上传,但每个组肯定是唯一的。并且类型设置为Magento\Config\Block\System\Config\Form\Field\File,但如果要上传图像,请记住类型Magento\Config\Block\System\Config\Form\Field\Image

最后,虽然你得到一个上传文件,它仍然无法正常工作。如下所示,建议您使用两件事。

  • 在此后端模块中,您应设置上载目录,检查允许的扩展名,验证文件大小并将文件路径保存到数据库。文件上载的默认后端模型是Magento\Config\Model\Config\Backend\File。然后让我们添加<upload_dir>- 上传目录来运行该文件。
<backend_model>Magento\Config\Model\Config\Backend\File</backend_model>
<upload_dir>upload</upload_dir>

从应用程序根目录,将放入文件上载magento_root/upload/。但是,当您将scope_info =“1”插入上载目录时,文件上载将保存到基于范围的位置。如果您应用默认范围,文件将在magento_root/upload/default。网站1将给我们magento_root/upload/websites/1/等运行配置以清除所有:

<section id="mageplaza_helloworld_section" translate="label" type="text" sortOrder="301" showInDefault="1" showInWebsite="1" showInStore="0">
    <label>Sales</label>
    <tab>sales</tab>
    <resource>Magento_Sales::config_sales</resource>
    <group id="custom_group" translate="label" type="text" sortOrder="6" showInDefault="1" showInWebsite="1" >
        <label>Security</label>
        <field id="custom_file_upload" translate="label" type="Magento\Config\Block\System\Config\Form\Field\File" sortOrder="6" showInDefault="1" showInWebsite="1" >
            <label>Upload custom file</label>
            <backend_model>Magento\Config\Model\Config\Backend\File</backend_model>
            <upload_dir config="system" scope_info="1">test</upload_dir>
        </field>
    </group>
</section>
  • 除了后端模型,您可以参考其他选项,如frontend_model(例如,用于自定义“拖放”文件上传),评论,工具提示,提示,验证等。

在Magento 2商店配置中上传文件时,接受许多类型的文件。但如果你想限制它,那是不允许的。为了做到,在system.xml文件中\Mageplaza\HelloWorld\Model\Config\Backend\CustomFileType,请看下面的例子,只包括csvxls感谢功能getAllowedExtensions()

<?php
 
namespace Mageplaza\HelloWorld\Model\Config\Backend;
 
class CustomFileType extends \Magento\Config\Model\Config\Backend\File
{
    /**
     * @return string[]
     */
    public function getAllowedExtensions() {
        return ['csv', 'xls'];
    }
}

第2步:刷新缓存并检查结果

刷新Magento缓存并检查结果。

这就对了!如果您对文章或任何问题有任何疑问,请使用下面的评论部分!

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论