file upload using knockout js

后端 未结 5 2745
故里飘歌
故里飘歌 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:31

    You can do the following:

    View :

    
    
    
    
    • :

    JS:

    var ViewModel = function() {
        var self = this;     
        self.files=  ko.observableArray([]);
        self.fileSelect= function (elemet,event) {
            var files =  event.target.files;// FileList object
    
            // Loop through the FileList and render image files as thumbnails.
            for (var i = 0, f; f = files[i]; i++) {
    
              // Only process image files.
              if (!f.type.match('image.*')) {
                continue;
              }          
    
              var reader = new FileReader();
    
              // Closure to capture the file information.
              reader.onload = (function(theFile) {
                  return function(e) {                             
                      self.files.push(new FileModel(escape(theFile.name),e.target.result));
                  };                            
              })(f);
              // Read in the image file as a data URL.
              reader.readAsDataURL(f);
            }
        };
    };
    
    var FileModel= function (name, src) {
        var self = this;
        this.name = name;
        this.src= src ;
    };
    
    ko.applyBindings(new ViewModel());
    

    You can find the demo in the link: http://jsfiddle.net/fPWFd/436/

提交回复
热议问题