uniapp APP 上传文件

/**
	 * 上传文件
	 */
	uploadPhoneFile:function(callback,params = {}) {
		let fileType = ['.pdf','.doc','.xlsx','.docx','.xls']
		// #ifdef APP-PLUS
			plus.io.chooseFile({
				title: '选择文件',  
				filetypes: ['doc', 'docx'], // 允许的文件类型  
				multiple: false, // 是否允许多选  
			},(e)=>{
				const tempFilePaths = e.files
				//第二步:把选择的文件上传到服务器
				Promise.all(tempFilePaths.map((tempFile) => {
					return new Promise((resolve,reject) => {
						uni.uploadFile({
							url: process.uniEnv.baseUrl + '/file/attch/upload',
							filePath: tempFile,
							name: 'file',
							header: {
								Authorization: 'Bearer ' + utils.CacheStorage("access_token"),//  uni.getStorageSync('access_token'),
								terminal:'app'
							},
							success: (uploadFileRes) => {
								// console.log('asdaggg',JSON.parse(uploadFileRes.data));
								let { code,data,msg } = JSON.parse(uploadFileRes.data)
								if(code === 200) {
									resolve(data)
								} else {
									uni.showToast({
										title: msg,
										icon: 'none'
									})
									reject('上传失败')
								}
							},
							fail: (error) => {
								reject(error)
							}
						});
					})
				})).then(images => {
					// console.log('上传成功>>>>>>',images)
					callback(images)
				})
			});
		// #endif
		
		// #ifndef APP-PLUS
		uni.chooseFile({
			//第一步:选择文件
			count: 1, //默认100
			extension: fileType,
			success: function (res) {
				const tempFilePaths = res.tempFilePaths;//若果只选择一个文件,这个文件就是数组的第一个元素
				//第二步:把选择的文件上传到服务器
				Promise.all(res.tempFilePaths.map((tempFile) => {
					return new Promise((resolve,reject) => {
						uni.uploadFile({
							url: '/dev-api/file/attch/upload',
							filePath: tempFile,
							name: 'file',
							header: {
								Authorization: 'Bearer ' + utils.CacheStorage("access_token"),//  uni.getStorageSync('access_token'),
								terminal:'app'
							},
							success: (uploadFileRes) => {
								console.log('asdaggg',JSON.parse(uploadFileRes.data));
								let { code,data,msg } = JSON.parse(uploadFileRes.data)
								if(code === 200) {
									resolve(data)
								} else {
									uni.showToast({
										title: msg,
										icon: 'none'
									})
									reject('上传失败')
								}
							},
							fail: (error) => {
								reject(error)
							}
						});
					})
				})).then(images => {
					console.log('asdjgllgl;s',images)
					callback(images)
				})
			}
		})
		// #endif
	}

写法很粗糙,只自己用作记录,可以再优化与扩展。

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用\[1\],在uniapp app上传腾讯COS文件可以使用upFile.js中的方法。在需要的页面中调用import upFile from '@/config/upFile.js'来引入upFile.js文件。然后可以使用upFileBtn(tag)方法来上传视频或者封面。如果tag等于1,表示上传视频,可以调用upFile.cosChoosevideo()方法来选择视频并上传。如果tag等于2,表示上传图片,可以调用upFile.cosUpLoadimg()方法来选择图片并上传上传成功后,可以获取到相应的视频链接或者封面图片链接。 根据引用\[2\],为了避免SDK版本变更引发的问题,可以在node_modules\cos-wx-sdk-v5\文件夹下使用Linux命令grep -rn 'wx.'来全局搜索其他使用小程序API的地方,并进行相应的替换。这样可以确保在上传腾讯COS文件时不会受到SDK版本变更的影响。 #### 引用[.reference_title] - *1* [uniapp----上传图片和视频到腾讯云COS](https://blog.csdn.net/heavenz19/article/details/129708899)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [uniapp+PHP,APP端使用腾讯云cos SDK上传文件](https://blog.csdn.net/weixin_35252964/article/details/116321982)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值