使用敲除js上传文件 [英] file upload using knockout js
本文介绍了使用敲除js上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用敲除js无法上传文件.我已经尝试使用以下代码,但无法正常工作.请提及我在哪里做错了.
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>
推荐答案
我为当前项目想出了这个解决方案.
I came up with this solution for my current project.
<img class="myImage" data-bind="attr: {src: $root.photoUrl() || 'images/tempImage.png'}"/>
<input data-bind="event: {change: $root.fileUpload}" type="file" accept="image/*" class="fileChooser"/>
<script>
var myController = function()
{
var self = this;
this.photoUrl = ko.observable();
this.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);
}
};
};
</script>
这篇关于使用敲除js上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文