|
|
@@ -485,6 +485,103 @@
|
|
|
form.attachments.splice(index, 1);
|
|
|
};
|
|
|
|
|
|
+ function copyToPublicAndOpen(sourcePath, fileName) {
|
|
|
+ // 获取 _downloads/ (公共下载目录) 的目录对象
|
|
|
+ plus.io.resolveLocalFileSystemURL(
|
|
|
+ '_downloads/',
|
|
|
+ entryDir => {
|
|
|
+ // 获取源文件对象
|
|
|
+ plus.io.resolveLocalFileSystemURL(
|
|
|
+ sourcePath,
|
|
|
+ entryFile => {
|
|
|
+ // 执行复制操作:将 sourcePath 复制到 _downloads/ 下,并重命名
|
|
|
+ entryFile.copyTo(
|
|
|
+ entryDir,
|
|
|
+ fileName,
|
|
|
+ newEntry => {
|
|
|
+ console.log('文件已复制到公共目录:', newEntry.fullPath);
|
|
|
+
|
|
|
+ uni.showToast({
|
|
|
+ title: '已保存到下载目录',
|
|
|
+ icon: 'none',
|
|
|
+ duration: 3000,
|
|
|
+ });
|
|
|
+
|
|
|
+ // 3. 预览打开
|
|
|
+ // 注意:打开公共目录的文件推荐用 plus.runtime.openFile
|
|
|
+ // uni.openDocument 有时对公共路径支持不好
|
|
|
+ plus.runtime.openFile(
|
|
|
+ newEntry.fullPath,
|
|
|
+ {},
|
|
|
+ e => {
|
|
|
+ console.log('打开成功');
|
|
|
+ },
|
|
|
+ e => {
|
|
|
+ console.error('打开失败', e);
|
|
|
+ uni.showToast({ title: '无法打开文件', icon: 'none' });
|
|
|
+ }
|
|
|
+ );
|
|
|
+ },
|
|
|
+ e => {
|
|
|
+ console.error('复制文件失败:', e);
|
|
|
+ uni.showToast({ title: '保存到公共目录失败', icon: 'none' });
|
|
|
+ }
|
|
|
+ );
|
|
|
+ },
|
|
|
+ e => {
|
|
|
+ console.error('读取源文件失败:', e);
|
|
|
+ }
|
|
|
+ );
|
|
|
+ },
|
|
|
+ e => {
|
|
|
+ console.error('读取下载目录失败:', e);
|
|
|
+ }
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ function saveTempFileToDownloads(tempPath, fileName) {
|
|
|
+ // 1. 获取系统 Downloads 目录对象
|
|
|
+ // "_downloads/" 是 H5+ API 对安卓公共下载目录的映射
|
|
|
+ plus.io.resolveLocalFileSystemURL(
|
|
|
+ '_downloads/',
|
|
|
+ entryDir => {
|
|
|
+ // 2. 获取临时文件对象
|
|
|
+ plus.io.resolveLocalFileSystemURL(
|
|
|
+ tempPath,
|
|
|
+ entryFile => {
|
|
|
+ // 3. 执行复制:将临时文件复制到 Downloads 目录
|
|
|
+ entryFile.copyTo(
|
|
|
+ entryDir,
|
|
|
+ fileName,
|
|
|
+ newEntry => {
|
|
|
+ console.log('文件路径:' + newEntry.fullPath);
|
|
|
+
|
|
|
+ uni.showToast({
|
|
|
+ title: '已保存至Downloads',
|
|
|
+ icon: 'none',
|
|
|
+ });
|
|
|
+
|
|
|
+ // 4. (可选) 打开预览
|
|
|
+ plus.runtime.openFile(newEntry.fullPath);
|
|
|
+ },
|
|
|
+ e => {
|
|
|
+ console.error('复制失败', e);
|
|
|
+ uni.showToast({ title: '保存失败', icon: 'none' });
|
|
|
+ }
|
|
|
+ );
|
|
|
+ },
|
|
|
+ e => {
|
|
|
+ console.error('读取临时文件失败', e);
|
|
|
+ }
|
|
|
+ );
|
|
|
+ },
|
|
|
+ e => {
|
|
|
+ console.error('无法访问下载目录', e);
|
|
|
+ // 这里如果报错,通常是权限没给或者 Android 11+ 读写受限
|
|
|
+ }
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
// 下载文件
|
|
|
const downloadFile = async file => {
|
|
|
console.log('🚀 ~ downloadFile ~ file:', file);
|
|
|
@@ -504,23 +601,25 @@
|
|
|
success: res => {
|
|
|
console.log('🚀 ~ downloadFile ~ res:', res);
|
|
|
if (res.statusCode === 200) {
|
|
|
- uni.saveFile({
|
|
|
- tempFilePath: res.tempFilePath,
|
|
|
- success: res => {
|
|
|
- console.log('🚀 ~ downloadFile saveFile ~ res:', res);
|
|
|
- uni.showToast({
|
|
|
- title: t('operation.downloadSuccess'),
|
|
|
- icon: 'none',
|
|
|
- });
|
|
|
- },
|
|
|
- fail: err => {
|
|
|
- console.log('🚀 ~ downloadFile saveFile ~ err:', err);
|
|
|
- uni.showToast({
|
|
|
- title: t('operation.downloadFail'),
|
|
|
- icon: 'none',
|
|
|
- });
|
|
|
- },
|
|
|
- });
|
|
|
+ saveTempFileToDownloads(res.tempFilePath, fileName);
|
|
|
+ // uni.saveFile({
|
|
|
+ // tempFilePath: res.tempFilePath,
|
|
|
+ // success: res => {
|
|
|
+ // // console.log('🚀 ~ downloadFile saveFile ~ res:', res);
|
|
|
+ // uni.showToast({
|
|
|
+ // title: t('operation.downloadSuccess'),
|
|
|
+ // icon: 'none',
|
|
|
+ // });
|
|
|
+ // copyToPublicAndOpen(res.savedFilePath, fileName);
|
|
|
+ // },
|
|
|
+ // fail: err => {
|
|
|
+ // console.log('🚀 ~ downloadFile saveFile ~ err:', err);
|
|
|
+ // uni.showToast({
|
|
|
+ // title: t('operation.downloadFail'),
|
|
|
+ // icon: 'none',
|
|
|
+ // });
|
|
|
+ // },
|
|
|
+ // });
|
|
|
}
|
|
|
},
|
|
|
fail: err => {
|
|
|
@@ -944,7 +1043,7 @@
|
|
|
<view class="file-list item-col" v-else>
|
|
|
<view v-for="(file, index) in form.attachments" :key="index">
|
|
|
<span>{{ file.filename }}</span>
|
|
|
- <!-- <button @click="downloadFile(file)" type="primary" size="mini" class="file-picker-btn">下载文件</button> -->
|
|
|
+ <button @click="downloadFile(file)" type="primary" size="mini" class="file-picker-btn">下载文件</button>
|
|
|
</view>
|
|
|
</view>
|
|
|
</uni-forms-item>
|