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
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/