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 unable to send the selected file from the client side to the server. Please suggest what is the best approach for this.
<input id="files" name="files" type="file" class="input-file" data-bind="file: FileProperties.FileName"/>
<button data-bind="click : Upload">Upload</button>
<script type="text/javascript">
ko.bindingHandlers.file = {
init: function (element, valueAccessor) {
alert('init');
$(element).change(function () {
var file = this.files[0];
if (ko.isObservable(valueAccessor())) {
valueAccessor()(file);
}
});
}
</script>
You can do the following:
View :
JS:
You can find the demo in the link: http://jsfiddle.net/fPWFd/436/
For Magento 2 below code is useful to display uploaded image by knockout js.
In html file
Js file need to code as below
above code is working fine with my project.
I came up with this solution for my current project.
Seems like you need a custom knockout binding for file uploading. There are various api/libs available that handles all the error cases with additional features. Try this: https://github.com/TooManyBees/knockoutjs-file-binding