uniapp上传

一. uniapp  uni-file-picker 上传(结合form)





二. uni.chooseImage

uni.chooseImage(OBJECT) | uni-app官网





uni.chooseImage({
	count: 1, //默认9 图片数量
    crop: { // 上传图片大小
        width: 80,
        height: 80,
    },
	sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
	success: function (e) {
		console.log(JSON.stringify(e.tempFilePaths));
        if(Math.ceil(e.tempFiles[0].size / 1024) > 10240) {
            uni.showToast({
                title: '头像大小不能超过10MB',
                icon: 'none'
            })
            return
        }
        this.uploadImage(e.tempFiles[0])
	}
});
uploadImage(imgObj) {
    let _this = this
    uni.uploadFile({
        url: this.configUrl + '/api/upload',
        filePath: img.path,
        name: 'file',
        formData: {},
        timeOut: 10000,
        success: (res)=> {
            console.log(res.data)
            let data = JSON.parse(res.data)
            if(data.isOk && data.data.path) {
                _this.activeData.headerImg = data.data.path
                uni.showToast({
                    icon: 'success',
                    title: '修改成功'
                })
            } else {
                uni.showToast({
                    icon: 'none',
                    title: data.message
                })
                return
            }
        },
        fail: (err)=> {
            uni.showToast({
               icon: 'none',
               title: '上传失败,请稍后再试'
            })
        }
    })
}

你可能感兴趣的:(uni-app,前端,小程序)