uni.uploadFile不兼容app

本文介绍如何在客户端使用uni-app下载服务器图片,然后将文件以兼容格式上传,涉及下载文件到临时路径并调用uploadFile函数进行上传操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求:登录的时候后端返回给我一个服务器图片地址,这时候我需要把图片传给另一个接口,但另一个接口只要文件格式的图片就…

以下格式是后端需要的 并且兼容app
在这里插入图片描述
先下载文件资源到本地(不会保留在手机上的),客户端直接发起一个 HTTP GET 请求,返回文件的本地临时路径。

		async downloadFiles() {
				let url = 'http服务器图片地址';
				let that = this;
				uni.downloadFile({
					url: url,
					success: function(res) {
						// 将临时文件传给后端
						var filePath = res.tempFilePath;
						if (res.statusCode === 200) {
							console.log(filePath)
							that.uploadFile(filePath)
						}
					},
					fail: (err) => {
						console.log(err);
					},
				});
			},

传给后端没了

			uploadFile(file) {
				console.log(file)
				console.log('进来了')
				uni.uploadFile({
					url: '7788521', //服务器地址
					fileType: "image",
					filePath: file, //临时文件
					name: 'cfName',
					header: {
						'authorization': "Bearer " + uni.getStorageSync("user_token")
					},
					success: (uploadFileRes) => {
						console.log('uploadFileRes')
						console.log(uploadFileRes)
					},
					fail: (res) => {
						console.log(res)
					}
				});

			},

我用的filePath,觉得我写法有问题的童鞋们可以随时留言评论
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值