ext-storage-qiniu.vue 5.1 KB
Newer Older
1 2 3 4 5 6 7
<template>
	<view class="content">
		<view class="title">直接上传文件到扩展存储-七牛云</view>
		<view class="tips">
			<text>
				各个小程序平台运行时,网络相关的 API 在使用前需要配置域名白名单。
			</text>
VK1688's avatar
VK1688 已提交
8
			<j-link text="参考" url="https://uniapp.dcloud.net.cn/uniCloud/ext-storage/dev.html#%E5%B0%8F%E7%A8%8B%E5%BA%8F%E5%9F%9F%E5%90%8D%E7%99%BD%E5%90%8D%E5%8D%95"></j-link>
9 10
		</view>
		<view class="btn-list">
VK1688's avatar
VK1688 已提交
11
			<button type="primary" plain @click="chooseImage(false)">选择文件“后”上传</button>
12
			<text class="tips">先调用uni.chooseImage选完文件/图片/视频后用uni.uploadFile方法上传</text>
VK1688's avatar
VK1688 已提交
13
			<button type="primary" plain @click="chooseImage(true)">上传并设为私有文件</button>
14 15 16 17 18 19 20 21 22 23
			<text class="tips">上传完成后设置为私有文件</text>
			<button type="primary" plain @click="getTempFileURL()" v-if="privateFileID">获取私有文件临时下载链接</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
A
Anne_LXM 已提交
24 25
				privateFileID: "",
				isTest:false
26 27 28 29
			}
		},
		mounted() {},
		methods: {
VK1688's avatar
VK1688 已提交
30
			chooseImage(isPrivate) {
31 32 33 34 35 36 37 38 39 40
				new Promise((resolve, reject) => {
					uni.chooseImage({
						count: 1,
						success: res => {
							const path = res.tempFilePaths[0]
							let ext
							// #ifdef H5
							ext = res.tempFiles[0].name.split('.').pop()
							const options = {
								filePath: path,
VK1688's avatar
VK1688 已提交
41 42
								cloudPath: Date.now() + '.' + ext,
								isPrivate
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
							}
							resolve(options)
							// #endif
							// #ifndef H5
							uni.getImageInfo({
								src: path,
								success(info) {
									const options = {
										filePath: path,
										cloudPath: Date.now() + '.' + info.type.toLowerCase()
									}
									resolve(options)
								},
								fail(err) {
									reject(new Error(err.errMsg || '未能获取图片类型'))
								}
							})
							// #endif
						},
						fail: () => {
							reject(new Error('Fail_Cancel'))
						}
					})
VK1688's avatar
VK1688 已提交
66 67
				}).then((options) => {
					this.uploadFile(options);
68 69 70 71 72 73 74 75 76 77 78
				}).catch((err) => {
					uni.hideLoading()
					console.log(err);
					if (err.message !== 'Fail_Cancel') {
						uni.showModal({
							content: `图片上传失败,错误信息为:${err.message}`,
							showCancel: false
						})
					}
				})
			},
VK1688's avatar
VK1688 已提交
79
			async uploadFile(options) {
A
Anne_LXM 已提交
80
				console.log('options: ',options);
VK1688's avatar
VK1688 已提交
81 82 83 84 85 86 87
				uni.showLoading({
					title: '文件上传中...'
				})
				const uniCloudStorageExtCo = uniCloud.importObject("ext-storage-co", {
					customUI: true
				});
				const uploadFileOptionsRes = await uniCloudStorageExtCo.getUploadFileOptions({
A
Anne_LXM 已提交
88
					cloudPath: `jest/${Date.now()}.jpg`, // 支持自定义目录
VK1688's avatar
VK1688 已提交
89 90
				});
				console.log('uploadFileOptionsRes: ', uploadFileOptionsRes)
A
Anne_LXM 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
				
				let testRes = new Promise((resolve,reject)=>{
					const uploadTask = uni.uploadFile({
						...uploadFileOptionsRes.uploadFileOptions, // 上传文件所需参数
						filePath: options.filePath, // 本地文件路径
						success: async () => {
							const res = {
								cloudPath: uploadFileOptionsRes.cloudPath, // 文件云端路径
								fileID: uploadFileOptionsRes.fileID, // 文件ID
								fileURL: uploadFileOptionsRes.fileURL, // 文件URL(如果是私有权限,则此URL是无法直接访问的)
							};
							// 上传成功后的逻辑
							console.log(res);
							if (options.isPrivate) {
								// 设为私有文件
								const uniCloudStorageExtCo = uniCloud.importObject("ext-storage-co", {
									customUI: true
								});
								await uniCloudStorageExtCo.setFilePrivate({
									fileID: res.fileID
								});
								this.privateFileID = res.fileID;
							}
VK1688's avatar
VK1688 已提交
114
							uni.showModal({
A
Anne_LXM 已提交
115
								content: '图片上传成功,fileID为:' + res.fileID,
VK1688's avatar
VK1688 已提交
116 117
								showCancel: false
							})
A
Anne_LXM 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
							resolve(res)
						},
						fail: (err) => {
							// 上传失败后的逻辑
							console.log(err);
							if (err.message !== 'Fail_Cancel') {
								uni.showModal({
									content: `图片上传失败,错误信息为:${err.message}`,
									showCancel: false
								})
							}
							reject(err)
						},
						complete: () => {
							uni.hideLoading()
VK1688's avatar
VK1688 已提交
133
						}
A
Anne_LXM 已提交
134 135 136 137 138 139 140 141
					});
					// 监听上传进度
					uploadTask.onProgressUpdate((res) => {
						console.log("监听上传进度", res);
					});
				})
				console.log("testRes",testRes)
				return testRes
VK1688's avatar
VK1688 已提交
142 143
			},
			async getTempFileURL() {
144 145
				const uniCloudStorageExtCo = uniCloud.importObject("ext-storage-co");
				let res = await uniCloudStorageExtCo.getTempFileURL({
VK1688's avatar
VK1688 已提交
146
					fileList: [this.privateFileID]
147
				});
VK1688's avatar
VK1688 已提交
148
				let tempFileURL = res.fileList[0].tempFileURL;
149 150 151 152
				uni.showModal({
					content: '图片临时下载链接为:' + tempFileURL,
					showCancel: false
				})
A
Anne_LXM 已提交
153 154 155
				if(this.isTest){
					return tempFileURL
				}
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
			}
		}
	}
</script>

<style>
	.content {
		padding-bottom: 30px;
	}

	.title {
		font-weight: bold;
		text-align: center;
		padding: 20px 0px;
		font-size: 20px;
	}

	.tips {
		color: #999999;
		font-size: 14px;
		padding: 20px 30px;
	}

	.btn-list {
		padding: 0px 30px;
	}

	.btn-list button {
		margin-top: 20px;
	}

	.upload-preview {
		width: 100%;
	}
VK1688's avatar
VK1688 已提交
190
</style>