当前位置:网站首页>Uniapp wechat applet selects chat log file upload
Uniapp wechat applet selects chat log file upload
2022-07-18 17:41:00 【Genting, Incubus】
Catalog
A condensed summary
A single file
wx.chooseMessageFile({
count: 1,// Limit the number of files selected
type: 'file',// Files other than pictures and videos , Don't choose to think all
//type:'video',// video
//type:'image',// picture
success (res) {
const tempFilePaths = res.tempFiles
uni.uploadFile({
url: 'https://....',
filePath: tempFilePaths[0].path,// Select a single path
name: 'file',// The backend needs to get this Key To get the contents of binary files
header:{
'X-Token':'Bearer ' + store.state.app.token
},
formData:{
'month':that.date
},
success: (uploadFileRes) => {
// The data returned by the interface
console.log(uploadFileRes.data);
// The status code returned by the interface
console.log(uploadFileRes.status);
}
});
}
})Multiple files
wx.chooseMessageFile({
count: 10,// Limit the number of files selected
type: 'file',// Files other than pictures and videos , Don't choose to think all
//type:'video',// video
//type:'image',// picture
success (res) {
const tempFilePaths = res.tempFiles
uni.uploadFile({
url: 'https://....',
files:tempFliePaths,// It takes arrays
header:{
'X-Token':'Bearer ' + store.state.app.token
},
formData:{
'month':that.date
},
success: (uploadFileRes) => {
// The data returned by the interface
console.log(uploadFileRes.data);
// The status code returned by the interface
console.log(uploadFileRes.status);
}
});
}
})PS:files and filePath/name Only one group can be selected
Example
This is used here xlsx File as an example , Select xlsx Upload the file to the specified interface .
let that=this// Retain vue example
wx.chooseMessageFile({
count: 1,// Limit the number of files selected
type: 'file',// Files other than pictures and videos , Don't choose to think all
extension:['xlsx','.xlsx'],// File types are restricted here
success (res) {
const tempFilePaths = res.tempFiles
console.log(' Temporary path ',tempFilePaths)
uni.uploadFile({
url: 'https://....',
filePath: tempFilePaths[0].path,
name: 'file',
header:{
'X-Token':'Bearer ' + store.state.app.token
},
formData:{
'month':that.date
},
success: (uploadFileRes) => {
// The data returned by the interface
console.log(uploadFileRes.data);
// The status code returned by the interface
console.log(uploadFileRes.status);
}
});
}
})Because some wechat versions extension It may not work , Or you want to verify the submitted file name , It is recommended to refer to my code below .
let that=this// Retain vue example
wx.chooseMessageFile({
count: 1,// Limit the number of files selected
type: 'file',// Files other than pictures and videos , Don't choose to think all
success (res) {
const tempFilePaths = res.tempFiles
console.log(' Temporary path ',tempFilePaths)
let filename = res.tempFiles[0].name
// Customized judgment file name , Judgment is not xlsx file , For reference only , It can be changed by itself
if(filename.lastIndexOf('.xlsx')==filename.length-5){
uni.uploadFile({
url: 'https://....',
filePath: tempFilePaths[0].path,
name: 'file',
header:{
'X-Token':'Bearer ' + store.state.app.token
},
formData:{
'month':that.date
},
success: (uploadFileRes) => {
// The data returned by the interface
console.log(uploadFileRes.data);
// The status code returned by the interface
console.log(uploadFileRes.status);
}
});
}else{
// Customized prompt file type error
uni.showToast({
title: ' Please select xlsx file ',
icon:'error',
duration: 2000
});
}
}
})Easy to step on
1、 Page refresh problem
call chooseMessageFile Will leave the page , When you return again , It will trigger the life cycle of each load , Such as uniapp Of onShow, If onShow Processed the data . be uni.uploadFile() Method call , The data is new . As in the code that.date.
2、extension problem
extension:['xlsx','.xlsx'],// File types are restricted here You may find that , Why are there two kinds of , Because ios There is nothing in the file extension management of “.”, If not compatible , be Ios End applet will make mistakes .
When there is no restriction on filtering ,ios And Andrew are shown in the figure below .


When there are correct restrictions , namely ".xlsx" and "xlsx", As shown in the figure below .


When limit error , No fit ios when , That is, no "xlsx"

You can see nothing , If Android doesn't fit ".xlsx", It should be similar , There is no test here .
If there are more pits , Welcome to comment .
边栏推荐
- 美称未对俄农产品“设障” 俄议员批美国是危机“始作俑者”
- MYSQL的主主/主从复制/xtrabackup/binlog恢复数据库以及使用ansible的常见模块
- Flutter realizes the gradual enlargement of hero pictures and the equal proportion method of pictures. Pictures are smoothly enlarged
- Njupt "Xin'an numeral base" Chapter 11 introduction to problem solving
- Safely terminate the operation of 2nodeos
- MFC|框架下按钮的自绘
- Competition of "four clouds"
- Which vite plug-ins can quickly provide development efficiency
- AB introduction to web performance testing tools
- V831——条形码识别
猜你喜欢
随机推荐
Alipay sandbox tests mobile website payment, prompting that the merchant's cooperation agreement has expired and cannot continue to be used
Data Lake (11): Iceberg table data organization and query
Given an integer array nums and a target value target, find the two integers whose sum is the target value in the array, and return their array subscripts.
Diabetes genetic risk testing challenge -toggle 30 days of ML
Penetration test tool - MSF generates backdoor immunity
SWD/JTAG Communication Failure和No Target Connected
TypeScript 类的使用
Flutter realizes the gradual enlargement of hero pictures and the equal proportion method of pictures. Pictures are smoothly enlarged
How to build cloud ide Theia on kubernetes platform
HCIA-R&S自用笔记(9)数据转发过程、单播/多播/组播
Basic permission management of Gerrit
给定一个整数数组nums和一个目标值target,在该数组中找出 和为 目标值的那两个整数,并返回他们的数组下标。
MFC|框架下按钮的自绘
27-Scala入门、基本数据类型和方法与函数的介绍
作用域、构造器详解
我回来了
文件解析漏洞详解
Etcd database source code analysis -- etcdserver bootstrap initialization cluster and raft
Scope and constructor details
Web性能测试工具之ab入门篇









