|
|
 代码:
- uni.downloadFile({
- url: this.article.fileUrl,
- success(res) {
- console.log(res)
- if (res.statusCode == 200) {
- let filePath = res.tempFilePath; // 下载临时路径
- // #ifdef APP || MP-BAIDU || MP-ALIPAY || MP-TOUTIAO || MP-XHS
- uni.saveFile({
- tempFilePath: filePath,
- success(t) {
- console.log(t)
- uni.showModal({
- title: '提示',
- content: '文件存储位置:'+t.savedFilePath,
- confirmText: '复制位置',
- success(e) {
- if (e.confirm) {
- uni.setClipboardData({
- data: t.savedFilePath,
- success() {
- uni.showToast({
- title: '复制成功',
- icon: 'none'
- })
- },
- fail() {
- uni.showToast({
- title: '复制失败'
- })
- }
- })
- }
- }
- })
- },
- fail() {
- uni.showModal({
- title: '提示',
- content: '文件保存失败'
- })
- }
- })
- // #endif
- // #ifdef MP-WEIXIN
- let FileSystemManager = uni.getFileSystemManager();
- FileSystemManager.saveFile({
- tempFilePath: filePath,
- success(t) {
- console.log(t)
- uni.showModal({
- title: '提示',
- content: '文件存储位置:'+t.savedFilePath,
- confirmText: '复制位置',
- success(e) {
- if (e.confirm) {
- uni.setClipboardData({
- data: t.savedFilePath,
- success() {
- uni.showToast({
- title: '复制成功',
- icon: 'none'
- })
- },
- fail() {
- uni.showToast({
- title: '复制失败'
- })
- }
- })
- }
- }
- })
- },
- fail() {
- uni.showModal({
- title: '提示',
- content: '文件保存失败'
- })
- }
- })
- // #endif
- }
- }
- })
复制代码 app端:
- saveFileToLocal(tempFilePath) {
- // 获取存储目录
- let fileName = tempFilePath.substring(tempFilePath.lastIndexOf('/') + 1);
- let savePath = plus.io.convertLocalFileSystemURL(`_downloads/${fileName}`); // 目标存储路径
-
- plus.io.resolveLocalFileSystemURL('_downloads/', (entry) => {
- entry.getFile(fileName, { create: true }, (fileEntry) => {
- fileEntry.createWriter((writer) => {
- writer.write(tempFilePath);
- console.log("文件保存成功:", savePath);
- }, (e) => console.error("写入失败", e));
- });
- });
- }
复制代码
|
|