添加本地连接下载,取消仅保存
This commit is contained in:
@@ -392,8 +392,8 @@
|
||||
uni.hideLoading();
|
||||
if (downloadRes.statusCode === 200) {
|
||||
const tempPath = downloadRes.tempFilePath;
|
||||
// 下载成功,保存到 yxd 目录
|
||||
saveFileToYxd(fileName, tempPath);
|
||||
// 下载成功,直接打开文件
|
||||
openFile(tempPath);
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: '下载失败',
|
||||
@@ -420,104 +420,6 @@
|
||||
}
|
||||
};
|
||||
|
||||
// 将下载的文件保存到外部存储 yxd 目录
|
||||
const saveFileToYxd = (fileName, tempPath) => {
|
||||
// #ifdef APP-PLUS
|
||||
// App 端:复制文件到 /storage/emulated/0/yxd/
|
||||
const YXD_PATH = '/storage/emulated/0/yxd/';
|
||||
|
||||
const ensureYxdDir = (callback) => {
|
||||
plus.io.resolveLocalFileSystemURL(YXD_PATH, (dirEntry) => {
|
||||
callback(dirEntry);
|
||||
}, () => {
|
||||
plus.io.resolveLocalFileSystemURL('/storage/emulated/0/', (rootEntry) => {
|
||||
rootEntry.getDirectory('yxd', {
|
||||
create: true
|
||||
}, (dirEntry) => {
|
||||
callback(dirEntry);
|
||||
}, (err) => {
|
||||
console.error('创建 yxd 目录失败:', JSON.stringify(err));
|
||||
uni.showToast({
|
||||
title: '创建目录失败',
|
||||
icon: 'error'
|
||||
});
|
||||
openFileConfirm(fileName, tempPath);
|
||||
});
|
||||
}, (err) => {
|
||||
console.error('访问外部存储根目录失败:', JSON.stringify(err));
|
||||
uni.showToast({
|
||||
title: '无法访问存储目录',
|
||||
icon: 'error'
|
||||
});
|
||||
openFileConfirm(fileName, tempPath);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
ensureYxdDir((yxdDirEntry) => {
|
||||
plus.io.resolveLocalFileSystemURL(tempPath, (fileEntry) => {
|
||||
fileEntry.copyTo(
|
||||
yxdDirEntry,
|
||||
fileName,
|
||||
() => {
|
||||
const targetPath = YXD_PATH + fileName;
|
||||
console.log('文件已保存到:', targetPath);
|
||||
openFileConfirm(fileName, targetPath);
|
||||
},
|
||||
(err) => {
|
||||
console.error('复制文件失败:', JSON.stringify(err));
|
||||
openFileConfirm(fileName, tempPath);
|
||||
}
|
||||
);
|
||||
}, (err) => {
|
||||
console.error('读取临时文件失败:', JSON.stringify(err));
|
||||
openFileConfirm(fileName, tempPath);
|
||||
});
|
||||
});
|
||||
// #endif
|
||||
// #ifndef APP-PLUS
|
||||
uni.saveFile({
|
||||
tempFilePath: tempPath,
|
||||
success: (saveRes) => {
|
||||
openFileConfirm(fileName, saveRes.savedFilePath);
|
||||
},
|
||||
fail: () => {
|
||||
openFileConfirm(fileName, tempPath);
|
||||
}
|
||||
});
|
||||
// #endif
|
||||
};
|
||||
|
||||
// 下载成功后确认是否打开文件
|
||||
const openFileConfirm = (fileName, filePath) => {
|
||||
uni.showActionSheet({
|
||||
title: `${fileName} 下载完成`,
|
||||
itemList: ['打开文件', '选择其他应用打开', '仅保存'],
|
||||
success: (res) => {
|
||||
switch (res.tapIndex) {
|
||||
case 0:
|
||||
openFile(filePath);
|
||||
break;
|
||||
case 1:
|
||||
openFileWithMenu(filePath);
|
||||
break;
|
||||
case 2:
|
||||
uni.showToast({
|
||||
title: '文件已保存到 yxd 目录',
|
||||
icon: 'success'
|
||||
});
|
||||
break;
|
||||
}
|
||||
},
|
||||
fail: () => {
|
||||
uni.showToast({
|
||||
title: '文件已保存到 yxd 目录',
|
||||
icon: 'success'
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// 使用系统默认应用打开
|
||||
const openFile = (filePath) => {
|
||||
uni.openDocument({
|
||||
|
||||
Reference in New Issue
Block a user