uniapp h5上传文件

                // #ifdef APP-PLUS || H5 
                var that = this;
                var x = document.createElement('input');
                x.setAttribute('type', 'file');
                x.click();
                x.onchange = function() {
                    console.log(this.files[0]);
                    var _this = this
                    var fData = new FormData();
                    fData.append("file", _this.files[0]);
                    fData.append("moduleName", 'reports');
                    var xhr = new XMLHttpRequest();
                    var surl = 'https://m.zbyep.com/Api/Common/uploadOssImg';
                    xhr.open("POST", surl, true);
                    xhr.onload = function(e) {
                        // console.log("上传成功", e); //上传成功
                        let response = JSON.parse(e.currentTarget.response)
                        that.atta_url = response.data; //获取服务端传过来的文件地址
                    };

                    xhr.setRequestHeader('enctype', "multipart/form-data");
                    xhr.send(fData)
                };
                // #endif 

你可能感兴趣的:(uniapp h5上传文件)