我是靠谱客的博主 丰富砖头,最近开发中收集的这篇文章主要介绍初学者随记01:uniapp开发调用微信API,实现在微信小程序上传、下载、打开单个文件功能,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
一、上传文件
一、上传文件
ChooseFile() {
let _this = this
wx.chooseMessageFile({
count: 1, //默认100
extension: ['.zip','.doc','.docx','.elsx','.xls','.ppt','.pptx','png','jpg','jpeg'],
success( res ) {
console.log(JSON.stringify(res))
_this.fileName = res.tempFiles[0].name // 上传的文件名称
if (res.tempFiles.length > 0){
uni.uploadFile({
url: `${_this.$Url}/receiving/addFile`,//this.$Url 开发中调用后台接口地址
filePath: res.tempFiles[0].path, //tempFiles存放选择的文件的本地临时文件对象数组
name: "file",
formData: {
file_name: "file",
token:_this.token
},
success ( res ) {
let ret = JSON.parse( res.data )
_this.fileSrc = ret.url //该返回的URL是调后台保存接口要传的路径
}
})
}
}
})
}
二、下载打开功能
downloadFile(){
let that = this
var fileurl = `能打开文件的完整路径`// 看后台返回路径的格式
wx.showLoading({
title: '加载中',
})
setTimeout(function(){
wx.hideLoading()
},2000)
wx.downloadFile({
url: fileurl,
success: function (res) {
const filePath = res.tempFilePath
var filename = fileurl;
var index1 = filename.lastIndexOf(".");
var index2 = filename.length;
var postf = filename.substring(index1, index2);//后缀名
var postf1 = postf.replace(/./g, '') // 截取的文件后缀,动态赋值打开文件的类型
console.log("下载成功")
if (filePath != null) {
//文件保存
uni.saveFile({
tempFilePath: filePath,
success: function(res) {
console.log("保存成功")
that.localPath = res.savedFilePath;
wx.openDocument({
filePath: res.savedFilePath, //这个字段值要赋值保存后的路径,不然文件打不开
fileType: postf1,
success: function (res) {
console.log('打开文档成功');
},
fail: function (res) {
console.log(res)
}
});
}
});
}
},
fail(err){
console.log('下载失败', err)
}
});
},
最后
以上就是丰富砖头为你收集整理的初学者随记01:uniapp开发调用微信API,实现在微信小程序上传、下载、打开单个文件功能的全部内容,希望文章能够帮你解决初学者随记01:uniapp开发调用微信API,实现在微信小程序上传、下载、打开单个文件功能所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复