Customer Import Plugin

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
1 year ago
I am trying to create a Customer Import widget plugin that replaces the import function that was removed however many versions ago (I'm on version 4.60). I have a ViewComponent and separate Controller to handle the import - this controller is inheriting the BaseAdminController class. The ViewComponent is invoked correctly and the widget is displaying correctly, but when I try to import a file, the Controller is not being hit and I'm seeing SSL errors in the console - I thought this may have been due to me not having '[AuthorizeAdmin]' or the '[Area(AreaNames.Admin)]' tags on the Controller/Controller methods but neither of these made a difference.

If anyone has any ideas / needs to see the code please let me know,
Thanks
1 year ago
Please provide more details on the errors you are seeing.
1 year ago
So the errors I'm getting when I try to execute the import is "Failed to load resource: net::ERR_SSL_PROTOCOL_ERROR" error.
This is the start of my controller:
namespace Nop.Plugin.Widgets.CustomerImport.Controllers
{
    [AutoValidateAntiforgeryToken]
    [Area(AreaNames.Admin)]
    public class CustomerImportController : BaseAdminController
    {
        #region Ctor

        public CustomerImportController(ICustomerAttributeService customerAttributeService, ICustomerAttributeFormatter customerAttributeFormatter,
        ICustomerAttributeParser customerAttributeParser, CustomerSettings customerSettings, IRepository<Customer> customerRepository,
        ICustomerService customerService, IEncryptionService encryptionService, IExportManager exportManager,
        IWorkContext workContext, IStoreContext storeContext, IMemoryCache cache, INopFileProvider fileProvider, IEmailSender emailSender,
        IEmailAccountService emailAccountService, EmailAccountSettings emailAccountSettings,
        DateTimeSettings dateTimeSettings,
        ForumSettings forumSettings,
            GdprSettings gdprSettings,
        IAddressAttributeParser addressAttributeParser,
            IAddressService addressService,
        ICustomerActivityService customerActivityService,
            ICustomerModelFactory customerModelFactory,
        ICustomerRegistrationService customerRegistrationService,
        IDateTimeHelper dateTimeHelper,
        IEventPublisher eventPublisher,
        IForumService forumService,
            IGdprService gdprService,
        IGenericAttributeService genericAttributeService,
            ILocalizationService localizationService,
        INewsLetterSubscriptionService newsLetterSubscriptionService,
            INotificationService notificationService,
        IPermissionService permissionService,
            IQueuedEmailService queuedEmailService,
        IRewardPointService rewardPointService,
        IStoreService storeService,
            ITaxService taxService,
            IWorkflowMessageService workflowMessageService,
        TaxSettings taxSettings, ICustomerImportManager customerImportManager)
        {
            _customerAttributeService = customerAttributeService;
            _customerAttributeFormatter = customerAttributeFormatter;
            _customerAttributeParser = customerAttributeParser;
            _customerSettings = customerSettings;
            _customerService = customerService;
            _customerRepository = customerRepository;
            _exportManager = exportManager;
            _workContext = workContext;
            _encryptionService = encryptionService;
            _storeContext = storeContext;
            _cache = cache;
            _fileProvider = fileProvider;
            _emailSender = emailSender;
            _emailAccountService = emailAccountService;
            _emailAccountSettings = emailAccountSettings;
            _gdprSettings = gdprSettings;
            _addressAttributeParser = addressAttributeParser;
            _addressService = addressService;
            _customerAttributeParser = customerAttributeParser;
            _customerRegistrationService = customerRegistrationService;
            _dateTimeHelper = dateTimeHelper;
            _customerImportManager = customerImportManager;
        }

        #endregion

        [HttpPost]
        [AuthorizeAdmin]
        public async Task<IActionResult> ImportCustomer(IFormFile uploadedFile)
        {
            if (uploadedFile == null || uploadedFile.Length <= 0)
            {
                return RedirectToAction("List", "Customer");
            }


And this is my widget view:
<button type="button" name="customerimportexcel" class="btn bg-olive" data-toggle="modal" data-target="#customerimportexcel-window">
    <i class="fas fa-upload"></i>
    @T("Admin.Common.Import")
  </button>


  @if (Model.HasImportErrors)
  {
    <img src="@Url.Content("~/Plugins/Widgets.CustomerImport/Content/images/import-error.png")"/>
  }
    <div id="customerimportexcel-window" class="modal fade import-cart-wrapper" tabindex="-1" role="dialog" aria-labelledby="customerimportexcel-window-title">
        <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-header">
                <h4 class="modal-title" id="customerimportexcel-window-title">@T("Admin.Common.ImportFromExcel")</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
              </div>
                <form asp-controller="CustomerImport" asp-action="ImportCustomer" method="post" enctype="multipart/form-data">
                    <div class="form-horizontal">
                        <div class="modal-body">
                            <ul class="common-list">
                                <li>
                                    <em>@T(ResourceNames.ImportTip)</em>
                                </li>
                                <li>
                                    <em>@T("Admin.Common.ImportFromExcel.ManyRecordsWarning")</em>
                                </li>
                            </ul>
                            <div class="form-group row">
                                <div class="col-md-2">
                                    <div class="label-wrapper">
                                        <label class="col-form-label">
                                            @T("Admin.Common.ExcelFile")
                                        </label>
                                    </div>
                                </div>
                                <div class="col-md-10">
                                    <input type="file" id="customerimportexcelfile" name="uploadedFile" class="form-control" asp-for="UploadedFile" />
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="submit" name="importcustomer" class="btn btn-primary">
                                @T("Admin.Common.ImportFromExcel")
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
1 year ago
Have you resolve it?
1 year ago
Yes , not sure why this solution didn't work but instead of using the default button submit event to get the uploaded file to the controller, I tried out a small script to find the file element and send the file that way. I added some custom routes to the RouteProvider and with this my CustomerImport is working:
const fileInput = document.getElementById('customerimportexcelfile');
    fileInput.addEventListener('change', function () {
        fileInput.click(function (event) {
            if (fileInput.files.length === 0) {
                console.log('No file selected');
                return;
            }

            console.log(fileInput.files[0]);

            const formData = new FormData();
            formData.append('file', fileInput.files[0]);
            $.ajax({
                url: '/CustomerImport/Import',
                type: 'POST',
                data: formData,
                cache: false,
                contentType: false,
                processData: false,
                success: function () {
                    window.location = '/CustomerImport/Import';
                }
            });
        });
    });
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.