简体   繁体   English

上传图片时如何获取图片路径

[英]How to get image path while uploading image

I have a requirement to upload an image and following is the HTML code 我需要上传图片,以下是HTML代码

<div class="fileupload fileupload-new" data-provides="fileupload" align="center">
                <div data-bind="if: doctor.imgSrc"> 
                    <div class="fileupload-new thumbnail" style="width: 150px; height: 150px;"><img data-bind="attr: { src: doctor.imgSrc }"/></div>
                </div>
                <div data-bind="ifnot: doctor.imgSrc">

                    <div data-bind="if: doctor.imagePath">
                    <div class="fileupload-new thumbnail" style="width: 150px; height: 150px;"><img data-bind="attr: { src: doctor.imagePath }"/></div>
                </div>
                <div data-bind="ifnot: doctor.imagePath">
                <div class="fileupload-new thumbnail" style="width: 150px; height: 150px;"><img src="${pageContext.request.contextPath}/resources/ui_resources/img/profile_pic.png" /></div>
                </div>
                </div>
                <div class="fileupload-preview fileupload-exists thumbnail" style="max-width: 150px; max-height: 150px; line-height: 20px;"></div>
                <div>
                <span class="btn btn-file"><span class="fileupload-new">Select image</span><span class="fileupload-exists">Change</span><input type="file" data-bind=" file: doctor.imgFile, fileObjectURL: doctor.imgSrc"/></span>
                <a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Remove</a>
                </div>
            </div>

and following is the JS codes 以下是JS代码

var Doctor = (function () {
    function Doctor(data) {
        this.id = ko.observable('');
        this.name = ko.observable('');
        this.degree = ko.observable('');
        this.gender = ko.observable('');
        this.username = ko.observable('');
        this.password = ko.observable('');
        this.email = ko.observable('');
        this.mobile = ko.observable('');
        this.imgFile = ko.observable('');
        this.imgSrc = ko.observable('');
        this.imagePath = ko.observable('');
        this.userid = ko.observable('');
        this.department = ko.observable('');
        this.schedules = ko.observableArray([]);
        if (data != null) {
            this.id = ko.observable(data['id']);
            this.name = ko.observable(data['name']);
            this.degree = ko.observable(data['degree']);
            this.gender = ko.observable(data['gender']);
            this.username = ko.observable(data['username']);
            this.password = ko.observable(data['password']);
            this.email = ko.observable(data['email']);
            this.mobile = ko.observable(data['mobile']);
            this.imgFile = ko.observable(data['imgFile']);
            this.imgSrc = ko.observable(data['imagePath']);
            this.imagePath = ko.observable(data['imagePath']);
            this.userid = ko.observable(data['userid']);
            this.department = ko.observable(data['department']);
        }
    }
    return Doctor;
})();

var doc = new Doctor(doctor); 
function vm() { 
var self = this; 
self.doctor = doc;
}
var viewModel = new vm(); 
ko.applyBindings(viewModel);

Now I have a save button and when its clicked it calls the following function 现在我有一个保存按钮,当单击它时,它将调用以下函数

$('#saveButton').click(function(){ 
alert('savebutton'); 
var testjson=ko.toJSON(viewModel.doctor); 
console.log(testjson);
});

The problem with it is I am not getting the imagepath,imgsrc and imgfile in the json. 它的问题是我没有在json中获取imagepath,imgsrc和imgfile。

This is the json 这是json

{"id":2,"name":"doc2","degree":"DA(Anaesthesia)","gender":"Female","username":"doc","password":"doc","email":"doc1@doc1.com","mobile":"12345678901","imgSrc":"imagePathValue","imagePath":"imagePathValue","userid":11,"department":"Bio-Chemistry","schedules":[{"id":"0","day":"Monday","fromtime":"17:21","totime":"18:21","hospitalId":5,"hospital":"Yashoda"},{"id":"1","day":"Tuesday","fromtime":"05:23","totime":"12:18","hospitalId":5,"hospital":"Yashoda"},{"id":"2","day":"Wednesday","fromtime":"11:23","totime":"12:23","hospitalId":5,"hospital":"Yashoda"},{"id":"3","day":"Thursday","fromtime":"17:27","totime":"18:27","hospitalId":46,"hospital":"Sai Bhavani"},{"id":"4","day":"Friday","fromtime":"10:30","totime":"12:30","hospitalId":46,"hospital":"Sai Bhavani"}]} 

Can anybody please point out the mistake I am doing? 有人可以指出我正在做的错误吗?

Update 更新

Following is the custom binding for file 以下是文件的自定义绑定

var windowURL = window.URL || window.webkitURL;

ko.bindingHandlers.file = {
    init: function(element, valueAccessor) {
        $(element).change(function() {
            var file = this.files[0];
            if (ko.isObservable(valueAccessor())) {
                valueAccessor()(file);
                formdata.append("image",file);
            }
        });
    },

    update: function(element, valueAccessor, allBindingsAccessor) {
        var file = ko.utils.unwrapObservable(valueAccessor());
        var bindings = allBindingsAccessor();

        if (bindings.fileObjectURL && ko.isObservable(bindings.fileObjectURL)) {
            var oldUrl = bindings.fileObjectURL();
            if (oldUrl) {
                windowURL.revokeObjectURL(oldUrl);
            }
            bindings.fileObjectURL(file && windowURL.createObjectURL(file));
        }

        if (bindings.fileBinaryData && ko.isObservable(bindings.fileBinaryData)) {
            if (!file) {
                bindings.fileBinaryData(null);
            } else {
                var reader = new FileReader();
                reader.onload = function(e) {
                    bindings.fileBinaryData(e.target.result);
                };
                reader.readAsArrayBuffer(file);
            }
        }
    }
};

My guess is that the data parameter you're using to initialize the Doctor does not contain an imgFile property. 我的猜测是,用于初始化Doctordata参数不包含imgFile属性。 Therefore, you're creating an observable on undefined -- and that won't show up in JSON. 因此,您要在undefined上创建一个observable,并且该值不会显示在JSON中。

Change the imgFile assignment to: imgFile分配更改为:

this.imgFile = ko.observable(data['imgFile'] || '');

Also, imgSrc and imgPath both grab their data from data['imagePath'] -- is that a copy/paste error? 另外, imgSrcimgPath data['imagePath']获取数据-这是复制/粘贴错误吗?

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

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