文件上传

       function createImageUploadItem(succ, errr) {
                var setting = {};
                setting.phoneSucc = function (result) {
                    //加载完毕
                    $scope.$apply(function () {
                        var data = JSON.parse(result.currentTarget.response);
                        if (data != null && data.Datas != null && data.Datas.length > 0) {
                            if (succ) succ(data.Datas[0].ImgUrl);
                            else {
                                this.model.PicUrl = data.Datas[0].ImgUrl;
                            }
                        }
                        model.com.closeloading();
                    }.bind(obj));
                }.bind(obj);

                setting.phoneError = function () {
                    this.picaddclick = this.picaddclickbase;
                    if (errr) errr();
                }.bind(obj);

                var obj = {
                    exp: function () {
                    },
                    model: {
                        isshowpicchoose: false,
                        PicUrl: '',//图片地址
                    },
                    picaddclick: function () {//图片添加点击
                        this.picaddclickbase();
                    },
                    picaddclickbase: function () {
                        console.log("clickbase")
                        //alert("clickabase4");
                        if (!this.filedom) {
                            var file = document.createElement("input");
                            file.type = "file";
                            file.accept = "image/*";//微信的话,只能这么调用
                            //"image/gif,image/jpeg,image/x-png";//限制选择的文件类型
                            //file.capture = "camera";//camera-拍照 
                            file.onchange = function () {
                                //alert("onchange")
                                if (this.files) {
                                    model.com.loading();
                                    //选择完毕了呗,那就上传呗
                                    var fd = new FormData();
                                    fd.append("fileToUpload", this.files[0]);
                                    var xhr = new XMLHttpRequest();
                                    xhr.upload.addEventListener("progress", function () {
                                        //加载中。。。
                                    }, false);
                                    xhr.addEventListener("load", function (result) {
                                        console.log("load");
                                        setting.phoneSucc(result);
                                    }, false);
                                    xhr.addEventListener("error", function () {
                                        console.log(setting);
                                        model.com.alert("网络错误")
                                    }, false);
                                    xhr.addEventListener("abort", function () {
                                        //中断上传
                                    }, false);
                                    xhr.open("POST", 'http://upload.eggworld.com.cn/' + 'api/uploadimage/eggworld_product_0_0_0_true');
                                    xhr.send(fd);
                                }
                            }
                            this.filedom = file;
                        }
                        this.filedom.click();
                    },
                    cancepicclick: function () {
                        this.model.isshowpicchoose = false;
                        this.picaddclick = this.picaddclickbase;
                    }
                };
                return obj;
            }

 

posted @ 2018-09-14 17:40  酱油帝君  阅读(148)  评论(0编辑  收藏  举报