简体   繁体   English

使用敲除将图像源与文件输入绑定

[英]Binding image source with file input using knockout

I have a image control and file input control, 我有一个图片控件和文件输入控件,

   <img data-bind="attr:{src:Picture}">
     <span><i class="fa fa-edit"></i></span><input type="file" name="Picturefile" data-bind="value: Picture"></label>
 <div class="btn btn-group">
            <button type="submit" value="save" data-bind="click: editClient" class="btn btn-primary"><i class="fa fa-ok"></i>Save!</button>
            <a href="/admin/client" class="btn btn-info"><i class="fa fa-arrow-left"></i>Back to list</a>
        </div>

On editing a entity, I want to bind the current picture with image control, also the user can edit and select a new picture using file control, 在编辑实体时,我想用图像控件绑定当前图片,用户也可以使用文件控件编辑并选择新图片,

Here is my javascript, 这是我的JavaScript,

<script type="text/javascript">
    $(function () {
        var ClientObject = {
            Name: "",
            Picture: ""

        }
        function Client(data) {
            var self = this;
            this.editClient = function () {
                var datatoPost = ko.mapping.toJS(self);
                $.ajax({
                    async: false,
                    method: 'post',
                    type: 'application/json',
                    url: '/api/my/client/',
                    data: datatoPost,
                    success: function (d) {
                        toastr.info("Saved");
                    }
                })
            }
            console.log('mapping to js',data);
            ko.mapping.fromJS(data, ClientObject, self);
        }

        var loadData = function (id) {
            var _url = '/api/v2/my/client';
            $.ajax({
                type: 'get',
                url: _url + '/' + id,
                success: function (d) {
                    if (!!d.error) { console.log(d.message); }
                    else {
                        var model = new Client(d);
                        ko.applyBindings(model, document.getElementById("clientCreate"));
                        console.log(model);
                    }
                }
            });
        }

        loadData(@ViewBag.Id);
    });
</script>

So on loading I get the following error: 所以在加载时出现以下错误:

Uncaught InvalidStateError: Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string. 

New to knockout !! 新淘汰赛!

I had to create a custom binding to do it. 我必须创建一个自定义绑定来做到这一点。 I haven't done any cross browser testing but I think it will work on all current browsers. 我尚未进行任何跨浏览器测试,但我认为它可以在所有当前浏览器上运行。

<input type="file" data-bind="fileSrc: src" id="file"/>
<img data-bind="attr:{src:src}"/>

$(function() {
    ko.applyBindings(new ViewModel());
});

var ViewModel = function () {
    var self = this;

    self.src = ko.observable();
};

ko.bindingHandlers.fileSrc = {
    init: function (element, valueAccessor) {
        ko.utils.registerEventHandler(element, "change", function () {
            var reader = new FileReader();

            reader.onload = function (e) {
                var value = valueAccessor();
                value(e.target.result);
            }

            reader.readAsDataURL(element.files[0]);
        });
    }
};

http://jsfiddle.net/5vfKZ/1/ http://jsfiddle.net/5vfKZ/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM