其他分享
首页 > 其他分享> > uni-app 上传文件

uni-app 上传文件

作者:互联网

<view class="img"  @tap="getUploadImg">
				<image :src='list.logo'></image>
			</view>
getUploadImg(e) {
				uni.chooseImage({
					count: 1, // 默认9
					sizeType: ['compressed'],
					sourceType: ['album'],
					success: (res) => {
						var tempFilePaths = res.tempFilePaths;
						uni.showLoading({
							mask: true
						})
						var params = {
							url: "/p/file/upload",
							filePath: tempFilePaths[0],
							name: 'file',
							callBack: (res2) => {
								uni.hideLoading();
								this.list.logo = JSON.parse(res2).resourcesUrl + JSON.parse(res2)
									.filePath,
									this._photoFiles = JSON.parse(res2).filePath
								this.isChange = true
							},
						};
						http.upload(params);
					},
					fail: (failMsg) => {
						console.log(failMsg)
					}
				})
			},

上传文件统一接口

/**
 * 上传文件统一接口
 */
function upload(params) {
    console.log(params)
    wx.uploadFile({
        url: config.domain + params.url,
        filePath: params.filePath,
        name: params.name,
        header: {
            'Authorization': params.login ? undefined : wx.getStorageSync('token')
        },
        formData: params.formData,
        dataType: 'json',
        responseType: params.responseType == undefined ? 'json' : params.responseType,
        success: function(res) {
			console.log(res,'------------')
            if (res.statusCode == 200) {
                //如果有定义了params.callBack,则调用 params.callBack(res.data)
                if (params.callBack) {
                    params.callBack(res.data);
                }
            } 
        },
        fail: function(err) {
            uni.hideLoading();
        }
    });
}

标签:filePath,res2,res,app,callBack,params,uni,上传
来源: https://blog.csdn.net/weixin_46210850/article/details/121905231