file upload using knockout js

后端 未结 5 2717
故里飘歌
故里飘歌 2021-02-20 04:39

File upload not working using knockout js. I have tried with below code but not working. Please mention where I am doing wrong.

This is my file control and button. I am

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-20 05:21

    For Magento 2 below code is useful to display uploaded image by knockout js.

    In html file

     
     
    

    Js file need to code as below

     define(['ko', 'uiComponent', 'jquery'], function (ko, Component, $) {
       'use strict';
        var photoUrl;
        return Component.extend({
          photoUrl : ko.observable(),
          fileUpload: function(data, e)
           {
              var file    = e.target.files[0];
              var reader  = new FileReader();
              reader.onloadend = function (onloadend_e)
              {
                var result = reader.result; // Here is your base 64 encoded file. Do with it what you want.
                self.photoUrl(result);
              };
              if(file)
              {
                reader.readAsDataURL(file);
              }
            },
          });
      });
    }
    

    above code is working fine with my project.

提交回复
热议问题