zoukankan      html  css  js  c++  java
  • 文件上传

           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;
                }
  • 相关阅读:
    CSS居中方法搜集
    函数表达式与函数声明的一点区别
    z-index 解析
    Android EditText的输入监听,输入字符的动态获取
    dispatchkeyevent的调用机制
    自定义带有图片的PreferenceActivity
    declare-styleable:自定义控件的属性
    在fragment中调用SharedPreferences
    使用Preference保存设置
    getSharedPreferences()与getSharedPreferences()与getDefaultSharedPreferences()的区别
  • 原文地址:https://www.cnblogs.com/zspbolg/p/9648006.html
Copyright © 2011-2022 走看看