uni-file-picker 自定义上传,显示上传进度条

1、自定义上传时,自定义一个回调方法,拿到progress,接下来会用到。

/**  
* @description 文件上传  
* @param data 文件路径  
* @param uuid 文件uuid  
* @param progressCallBack 上传进度回调  
*/ 
function uploadFile(data, uuid, progressCallBack) {
	var promise = new Promise((resolve, reject) => {
		var tempFilePaths = data;
		const uploadTask = uni.uploadFile({
			url: commoneUrl + "file/upload", //仅为示例,非真实的接口地址
			filePath: tempFilePaths,
			name: 'upFile',
			formData: {

			},
			success: async (res) => {

				console.log(res)
				// 获取图片list  
				const {
					data,
					statusCode
				} = res

				if (statusCode == 200) {
					resolve({
						url: data,
						uuid
					})
				}
			},
			fail: (res) => {
				reject(res);
			}
		});
		// 通过uploadTask 监听上传进度   
		uploadTask.onProgressUpdate(res => {
			progressCallBack(res)
		})
	});
	return promise;
}

2、上传文件时调用

//上传视频
        async selectVideo(e) {
            const _this = this
            const tempFilePaths = e.tempFilePaths[0];
            const uuid = e.tempFiles[0]['uuid']
            const res = await _this.api.upload(tempFilePaths, uuid, function(progress) {
                _this.$refs.uploadVideo.setProgressByCustomUplaod(uuid, progress
                    .progress) // 通过组件实例调用设置进度条的方法  
            })
            _this.formData.videoUrl = res.url
            _this.messageToggle('success', '视频上传成功!')

        },

3、在组件中设置ref,目的是得到组件实例。通过实例即可调用组件方法setProgress。为了与源代码区分,定义新的方法setProgressByCustomUplaod

<uni-file-picker  fileMediatype="video" mode="grid" @select="selectVideo"
						ref="uploadVideo" @delete="deleteVideo" :limit="1"  />

调用:_this.$refs.uploadVideo.setProgressByCustomUplaod(uuid, progress.progress)

4、在uni_modules/uni-file-picker/components/uni-file-picker/uni-file-picker.vue文件中新增方法setProgressByCustomUplaod

/**  
 * @description 自定义上传时,设置进度条状态  
 * @param uuid 文件uuid,目的是通过uuid从文件列表中取出当前正在上传的文件  
 * @param progress 上传进度  
 */
setProgressByCustomUplaod(uuid, progress) {
    const currentFile = this.files.find(item => item.uuid == uuid)
    if (currentFile) {
        currentFile.progress = progress
    }
},

原文地址:https://ask.dcloud.net.cn/question/153368

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
自定义 uni-file-picker 的样式,你可以使用以下步骤: 1. 在你的项目中创建一个名为 `uni-file-picker` 的文件夹,用于存放自定义样式文件。 2. 在 `uni-file-picker` 文件夹中创建一个名为 `uni-file-picker.vue` 的文件,用于编写自定义uni-file-picker 组件。 3. 在 `uni-file-picker.vue` 文件中,引入 uni-file-picker 组件,并根据你的需求进行样式修改。你可以使用 CSS 或者 SCSS 来编写样式。 4. 在需要使用 uni-file-picker 的页面中,引入自定义的 `uni-file-picker.vue` 组件,并替换原有的 uni-file-picker 组件。 以下是一个简单的示例,演示如何自定义 uni-file-picker 的样式: 1. 在 `uni-file-picker` 文件夹中创建 `uni-file-picker.vue` 文件,内容如下: ```html <template> <div class="custom-file-picker"> <div class="custom-button" @click="openFilePicker">选择文件</div> <input type="file" ref="fileInput" style="display: none" @change="handleFileChange" /> <div class="custom-selected-file">{{ selectedFile }}</div> </div> </template> <script> export default { data() { return { selectedFile: '' }; }, methods: { openFilePicker() { this.$refs.fileInput.click(); }, handleFileChange(event) { const file = event.target.files[0]; this.selectedFile = file.name; // 处理文件上传逻辑 } } }; </script> <style scoped> .custom-file-picker { /* 自定义样式 */ } .custom-button { /* 自定义按钮样式 */ } .custom-selected-file { /* 自定义选中文件样式 */ } </style> ``` 2. 在需要使用 uni-file-picker 的页面中,引入自定义的 `uni-file-picker.vue` 组件,并替换原有的 uni-file-picker 组件。例如: ```html <template> <div> <uni-file-picker></uni-file-picker> </div> </template> <script> import UniFilePicker from '@/components/uni-file-picker/uni-file-picker.vue'; export default { components: { UniFilePicker } }; </script> ``` 这样就可以通过自定义 `uni-file-picker.vue` 组件来实现上传文件的自定义样式。你可以根据需要修改 `uni-file-picker.vue` 文件中的样式和逻辑。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值