uniapp多附件上传plus+h5分开处理

这样写是因为后面上传时plus环境中addFile必须是本地路径,不支持添加网络资源

//这里是获取历史上传的图片数组,下载到本地获取临时地址回显
GetWoCheckFileList() {
				uni.showLoading({
					mask: true
				})
				this.loading = true;
				let _that = this;
				GetFileList({
					wo: _that.WO_NO
				}).then(res => {
					if (res.code == 0) {
						const tempData = res.data || []
						_that.fileList = []
						for (let item of tempData) {
							_that.GetFilePath(item.OriginFilePath).then(fileRes => {
								// console.log(fileRes);
								_that.fileList.push({
									...item,
									name: item.FileName,
									url: fileRes.tempFilePath
								});
							}).catch(res => {
								uni.$u.toast(res)
							})
						}
					} else {
						uni.$u.toast('未获取到工单检验详情')
					}
				}).catch(res => {
					uni.$u.toast(res)
				}).finally(() => {
					this.loading = false;
					uni.hideLoading()
					this.$nextTick(() => {
						this.$refs.collapse.init()
					})
				})
			},
			GetFilePath(url) {
				return new Promise((resolve, reject) => {
					uni.downloadFile({
						url: baseFileUrl + "/" + url,
						success: (downloadResult) => {
							resolve(downloadResult)
						},
						fail: (err) => {
							reject(err)
						}
					})
				})
			},

以下是处理上传的方法

async handleSaveFileList() {
				this.loading = true
				try {
					uni.showLoading({
						title: '加载中..',
					});
					let _that = this
					const submitData = {
						wo: _that.WO_NO,
						checkUser: _that.$store.getters.user.LOGINNAME
					}
					if (!_that.fileList.length) {
						return uni.$u.toast('未获取到需要上传的附件')
					}
					const params = uni.$u.queryParams(submitData)
					// #ifndef APP-PLUS
					for (var i = 0; i < this.fileList.length; i++) {
						if (this.fileList[i].url) {
							let tempFIle = await blobUrlToFile(this.fileList[i].url, this.fileList[i].FileName)
							submitData['files[' + i + ']'] = tempFIle
						}
					}
					// app端必须使用一个正确的地址获取到文件上传 不然会报错
					uni.uploadFile({
						url: `${_that.$store.getters.apiUrl}/PDA/SaveCheckFiles${params}`,
						filePath: `${_that.$store.getters.apiUrl}/Content/lib/layui-v2.6.3/font/iconfont.ttf`,
						name: 'tempFile',
						formData: submitData,
						success: (uploadFileRes) => {
							try {
								const res = JSON.parse(uploadFileRes.data)
								if (res.code == 0) {
									_that.$u.toast(res.msg)
									_that.GetWoCheckFileList()
								} else {
									_that.$u.toast(res.msg)
								}
							} catch (e) {
								_that.$u.toast('接口异常!请联系管理员')
							}
						},
						fail() {
							_that.$u.toast('提交异常,请联系管理员处理')
						},
						complete() {
							uni.hideLoading()
							_that.loading = false
						}
					})
					// #endif
					// #ifdef APP-PLUS
					var task = plus.uploader.createUpload(
						`${_that.$store.getters.apiUrl}/PDA/SaveCheckFiles${params}`, {
							method: "POST"
						},
						function(t, status) { //上传完成
							uni.hideLoading()
							try {
								if (status == 200) {
									const res = JSON.parse(t.responseText)
									if (res.code == 0) {
										_that.$u.toast(res.msg)
										_that.GetWoCheckFileList()
									} else {
										_that.$u.toast(res.msg)
									}
								} else {
									_that.ErrorHtml = t.responseText
									_that.IsNeedYSshow = true
									_that.$u.toast("提交失败:" + status);
								}
							} catch (e) {
								_that.$u.toast('接口异常!请联系管理员')
							}
						}
					);

					for (var i = 0; i < this.fileList.length; i++) {
						if (this.fileList[i].url) {
							task.addFile(this.fileList[i].url, {
								key: 'files[' + i + ']'
							});
						}
					}
					const keys = Object.keys(submitData)
					keys.forEach(key => {
						task.addData(key, submitData[key] + '');
					})
					//task.addEventListener( "statechanged", onStateChanged, false );
					task.start();
					// #endif

				} catch (e) {
					uni.hideLoading()
					this.$u.toast(e.message)
					this.loading = false
				}
			},
			

补一个方法

export function blobUrlToFile(blobUrl, fileName) {
	if (!blobUrl) return;
	return new Promise((resolve, reject) => {
		const xhr = new XMLHttpRequest();
		xhr.open('GET', blobUrl);
		xhr.responseType = 'blob';

		xhr.onload = () => {
			if (xhr.status === 200) {
				const blob = xhr.response;
				const file = new File([blob], fileName);
				resolve(file);
			} else {
				reject(new Error('Failed to convert Blob URL to File.'));
			}
		};

		xhr.onerror = () => {
			reject(new Error('Failed to convert Blob URL to File.'));
		};

		xhr.send();
	});
}
  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值