uniapp+uview实现图片的上传预览,携带额外参数,headers


可以直接按照官网给的例子来,需要注意的就是额外携带参数的话要如何配置,我这里的 upFileData,和 headers是直接写在data里边的 headers 是在 onload 中获取了localStorage的token

一、携带额外参数(uview 1x)

:form-data="upFileData" :header="headers"

在这里插入图片描述
在这里插入图片描述

一、携带额外参数(uview 2x)

修改官网 给出的例子 中的uploadFilePromise方法 ,携带请求头和 额外参数都是在这里改,直接加在属性上无效(按照uview 1x的方法无效)

    header: this.headers,
	name: 'file',
	formData: {
		bussinessId: this.info.id,
		type: 'photo'
    },
uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.action, // 仅为示例,非真实的接口地址
						filePath: url,
						header: this.headers,
						name: 'file',
						formData: {
							bussinessId: this.info.id,
							type: 'photo'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
<u-modal v-model="upFile" title="" :async-close="false" :mask-close-able="true" :show-confirm-button="false">
	<!-- 上传 -->
	<u-upload style="margin: 0 0 20px 20px;" :action="action" :fileList="fileListfile" @afterRead="afterRead"
		:form-data="upFileData" :header="headers" @delete="deletePic" @on-success="success" name="file" multiple
		:maxCount="10">
	</u-upload>
</u-modal>

二、预览图片

<!-- 预览的弹窗 -->
<u-modal v-model="lookFile" title="" :async-close="false" :mask-close-able="true" :show-confirm-button="false">
	<view class="fileBox">
		<u-image class="fileBoxImg" v-for="ite in prewList" :showLoading="true" :src="getFullUrl(ite.id)"
			width="80px" height="80px" @click="lookPhoto(ite.id)"></u-image>
	</view>
</u-modal>

主要是使用 uni.previewImage

lookPhoto(id) {
		//预览图片
		var urlList = []
		let url = 'http://cczs.xxxx.com/ccbg/sys/cloud/file/xxxx?id=' + id  //可以直接查看图片的地址
		urlList.push(url) //push中的参数为 :src="" 中的图片地址
		uni.previewImage({
		urls: urlList
	})
},

三、提示填写请求路径

:action="http://cczs.xxxx.com/ccbg/sys/cloud/file/upload"

uview官网 :https://www.uviewui.com/components/upload.html

在这里插入图片描述
在这里插入图片描述

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

codernmx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值