uniapp 微信小程序 web-view上传手机文件管理器里最近文件(限制文件类型)
文本限制用户上传的文件类型为:office、pdf、图片。效果:<template><div class="container"><van-uploader accept="*" :before-read="beforeRead" :max-count="1" :max-size="10 * 1024 * 1024" @oversize="onOversize" :a
·
本文限制用户只能上传的文件类型为:
office、pdf、图片。
本文的web-view中使用vant.js的van-uploader组件,使用该组件自带的api来获取文件和限制文件尺寸和类型
界面效果:


重要提示:
在vant.js的van-uploader组件的before-read中拿不到file对象。也拿不到file.path对象,但是可以拿到file.type和file.name。
<template>
<div class="container">
<van-uploader accept="*" :before-read="beforeRead" :max-count="1" :max-size="10 * 1024 * 1024" @oversize="onOversize" :after-read="afterRead">
<!-- <div class="box-button">
<div>
<div class="icon-image">
<img
src="@/assets/images/add.png"
class="image-style"
/>
</div>
<div class="box-button-text">添加文件</div>
</div>
</div> -->
<div>
<van-button round icon="plus" type="info">上传文件</van-button>
</div>
</van-uploader>
</div>
</template>
<script type="text/javascript" src="jsfile/weixin.js"></script>
<script>
import { uploadFile } from '@/api/mobileUpload.js'
import { setToken } from '@/utils/auth'
import { Toast } from 'vant';
export default {
data() {
return {
fileList: [],
isFile: [
"image/jpeg",
"image/png",
"application/msword",
"application/vnd.ms-excel",
"application/pdf",
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
],
};
},
created() {
console.log('拿到的参数1:', this.$route.query, window.location)
console.log('拿到的参数2:', this.$route.query.token)
},
methods: {
beforeRead(file) {
if(file.name){
const fileExtension = ['png','jpg','jpeg','doc','docx','pdf','xls','xlsx','ppt','pptx']
// const fileFormat = ['image/jpeg','image/png','application/msword','application/vnd.ms-excel','application/pdf','application/vnd.openxmlformats-officedocument.wordprocessingml.document','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet','application/vnd.openxmlformats-officedocument.presentationml.presentation']
const arr = file.name.split('.')
const extensionName = arr[arr.length-1]
// const index = fileFormat.indexOf(file.type)
const index = fileExtension.indexOf(extensionName)
if (index===-1) {
Toast("仅支持上传图片、office、pdf格式的文件")
return false;
}
return true;
} else {
return true
}
},
// 所上传的文件超过尺寸事件
onOversize() {
Toast('文件大小不能超过 10M');
},
afterRead(param){
console.log('拿到的文件消息:', param)
const file = param.file
// this.test2 = JSON.stringify(file.path)
// this.test = JSON.stringify(file)
// const fileExtension = ['png','jpg','jpeg','doc','docx','pdf','xls','xlsx','ppt','pptx']
// const arr = file.path.split('.')
// const extensionName = arr[arr.length-1]
// const index = fileExtension.indexOf(extensionName)
// if (index===-1) {
// Toast("仅支持上传图片、office、pdf格式的文件2")
// return
// }
const str = this.$route.query.token
setToken(str)
console.log('文件信息', file)
const formData = new FormData()
formData.append('name', file.name)
formData.append('file', file)
uni.getEnv(res => {
console.log('当前环境1:', res);
// 如果是微信小程序环境
if(res.miniprogram){
console.log('确认是微信小程序运行环境')
// 上传后端
uploadFile(formData).then(response => {
const data = response.data
// 如果上传成功
if (data.code == 200) {
console.log('上传成功', data)
// 转到小程序首页
uni.postMessage({
data: {
mobileResult: data
}
});
uni.reLaunch({
url: '/pages/home/index?mobileResult=' + encodeURIComponent(JSON.stringify(data))
});
} else {
// 如果上传失败
console.log('文件上传失败',response)
uni.postMessage({
data: {
mobileResult: data
}
});
uni.reLaunch({
url: '/pages/home/index?mobileResult=' + encodeURIComponent(JSON.stringify(data)),
animationType: 'none',
animationDuration: 0
});
}
})
} else {
// 考虑从浏览器直接进入的情况
Toast('请从微信小程序进入');
}
})
}
},
};
</script>
<style scoped>
.container {
width: 100%;
height: calc(100vh - 80px);
display: flex;
justify-content: center;
align-items: center;
box-sizing: border-box;
}
.image-style{
width: 30px;
height: 30px
}
.box-button {
width: 100px;
height: 100px;
background: rgb(18, 125, 250);
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
}
.box-button-text {
color: white;
font-size: 13px;
}
.box-button:active {
width: 100px;
height: 100px;
background: red;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
}
::v-deep .el-upload-dragger {
background-color: #eaecf3;
border: none;
}
</style>
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐

所有评论(0)