uniapp文件上传、下载
直接上代码
1、文件下载的
<view>
<text calss="download(index)">下载保存</text>
</view>
download(index) {
var _this = this
uni.downloadFile({
url: _this.list[index].fileUrl, //仅为示例,并非真实的资源
success: (res) => {
var path = res.tempFilePath
console.log(path);
if (res.statusCode === 200) {
// preview
uni.openDocument({
filePath: path,
success: function(res) {
console.log('打开文档成功');
console.log(res);
}
});
//文件保存
uni.saveFile({
tempFilePath: path,
success: function(res) {
var savedFilePath = res.savedFilePath;
console.log(savedFilePath)
uni.showToast({
title: '下载成功',
content: savedFilePath,
duration: 500
});
uni.getSavedFileList({
success: function(res) {
console.log(res.fileList);
}
});
}
});
}
}
});
},
2、文件上传(安卓)
<tki-file-manager ref="filemanager" @result="resultPath"></tki-file-manager>
<view class="openFile">
<button class="uploadBtn" type="primary" @click="openFile()">立即上传</button>
<view ref="input" class="input"></view>
</view>
//引入组件
import tkiFileManager from "@/components/tki-file-manager.vue"
methods: {
//打开手机文件
openFile() {
var _this = this
_this.$refs.filemanager._openFile()
},
resultPath(e) {
let _this = this
_this.path = e
const uploadTask = uni.uploadFile({
//url: 'http://192.168.0.188:8080/f/craftsman/article/uploadArticleFile', //仅为示例,
filePath: "file:/" + _this.path,
name: 'file',
formData: { //参数可以根据真实接口写
'articleId': _this.articleId + '',
'fileTitle ': '123',
'fileSubtitle': '123',
'fileType': '3'
},
success: function(res) {
console.log(res.data.success);
console.log(_this.path)
var data = JSON.parse(res.data);
}
});
},
}
可以交流!!!!