| | |
| | | <u-button size="small" |
| | | type="info" |
| | | plain |
| | | @click="downloadFile(file)">下载</u-button> |
| | | @click="downloadFile(file)">下载并预览</u-button> |
| | | <u-button size="small" |
| | | type="error" |
| | | plain |
| | |
| | | <text class="empty-text">暂无附件</text> |
| | | </view> |
| | | </view> |
| | | <a rel="nofollow" |
| | | <!-- <a rel="nofollow" |
| | | id="downloadLink" |
| | | href="#" |
| | | style="display:none;">下载文本文件</a> |
| | | style="display:none;">下载文本文件</a> --> |
| | | <!-- 上传按钮 --> |
| | | <view class="upload-button" |
| | | @click="chooseFile"> |
| | |
| | | import PageHeader from "@/components/PageHeader.vue"; |
| | | import config from "@/config"; |
| | | import { getToken } from "@/utils/auth"; |
| | | import axios from "axios"; |
| | | import requestApp from "@/utils/requestApp"; |
| | | // import { saveAs } from "file-saver"; |
| | | import { |
| | | listRuleFiles, |
| | |
| | | |
| | | // 选择文件 |
| | | const chooseFile = () => { |
| | | // uni.chooseImage({ |
| | | // count: 9, |
| | | // sizeType: ["original", "compressed"], |
| | | // sourceType: ["album", "camera"], |
| | | // success: res => { |
| | | // uploadFiles(res.tempFiles); |
| | | // }, |
| | | // fail: err => { |
| | | // console.error("选择图片失败:", err); |
| | | // showToast("选择文件失败"); |
| | | // }, |
| | | // }); |
| | | uni.chooseImage({ |
| | | count: 9, |
| | | sizeType: ["original", "compressed"], |
| | | sourceType: ["album", "camera"], |
| | | success: res => { |
| | | console.log(res, "选择图片成功"); |
| | | uploadFiles(res.tempFiles); |
| | | }, |
| | | fail: err => { |
| | | console.error("选择图片失败:", err); |
| | | showToast("选择文件失败"); |
| | | }, |
| | | }); |
| | | // uni.chooseFile({ |
| | | // count: 9, |
| | | // extension: [ |
| | |
| | | // ".rar", |
| | | // ], |
| | | // success: res => { |
| | | // console.log(res, "选择文件成功"); |
| | | // uploadFiles(res.tempFiles); |
| | | // }, |
| | | // fail: err => { |
| | |
| | | |
| | | // 上传文件 |
| | | const uploadFiles = tempFiles => { |
| | | console.log(tempFiles, "上传文件1"); |
| | | tempFiles.forEach((tempFile, index) => { |
| | | // 显示上传中提示 |
| | | uni.showLoading({ |
| | | title: "上传中...", |
| | | mask: true, |
| | | }); |
| | | console.log(tempFile, "上传文件"); |
| | | console.log(tempFile, "上传文件2"); |
| | | // 1. 直接使用 uni.uploadFile 上传文件 |
| | | uni.uploadFile({ |
| | | url: config.baseUrl + "/file/upload", |
| | |
| | | }, |
| | | success: uploadRes => { |
| | | uni.hideLoading(); |
| | | console.log(uploadRes, "上传文件3"); |
| | | |
| | | try { |
| | | const res = JSON.parse(uploadRes.data); |
| | | console.log(res, "上传文件4"); |
| | | if (res.code === 200) { |
| | | // 2. 提取文件信息 |
| | | const fileName = tempFile.name; |
| | | const fileType = fileName.split(".").pop(); |
| | | const fileName = tempFile.name |
| | | ? tempFile.name |
| | | : tempFile.path.split("/").pop(); |
| | | // const fileType = fileName.split(".").pop(); |
| | | // 3. 构造保存文件信息的参数 |
| | | const saveData = { |
| | | name: fileName, |
| | |
| | | }); |
| | | }); |
| | | }; |
| | | |
| | | // 下载文件 |
| | | const downloadFile = file => { |
| | | var url = |
| | |
| | | header: { Authorization: "Bearer " + getToken() }, |
| | | }) |
| | | .then(res => { |
| | | console.log(res, "res"); |
| | | const isBlob = blobValidate(res.data); |
| | | console.log(isBlob, "isBlob"); |
| | | if (isBlob) { |
| | | const blob = new Blob([res.data], { type: "text/plain" }); |
| | | const url = URL.createObjectURL(blob); |
| | | const downloadLink = document.getElementById("downloadLink"); |
| | | downloadLink.href = url; |
| | | downloadLink.download = file.name; |
| | | downloadLink.click(); |
| | | // downloadLink.style.display = "block"; |
| | | showToast("下载成功"); |
| | | let osType = uni.getStorageSync("deviceInfo").osName; |
| | | let filePath = res.tempFilePath; |
| | | if (osType === "ios") { |
| | | uni.openDocument({ |
| | | filePath: filePath, |
| | | showMenu: true, |
| | | success: res => { |
| | | resolve(res); |
| | | }, |
| | | fail: err => { |
| | | console.log("uni.openDocument--fail"); |
| | | reject(err); |
| | | }, |
| | | }); |
| | | } else { |
| | | showToast("下载失败"); |
| | | uni.saveFile({ |
| | | tempFilePath: filePath, |
| | | success: fileRes => { |
| | | uni.showToast({ |
| | | icon: "none", |
| | | mask: true, |
| | | title: |
| | | "文件已保存:Android/data/uni.UNI720216F/apps/__UNI__720216F/" + |
| | | fileRes.savedFilePath, //保存路径 |
| | | duration: 3000, |
| | | }); |
| | | setTimeout(() => { |
| | | //打开文档查看 |
| | | uni.openDocument({ |
| | | filePath: fileRes.savedFilePath, |
| | | success: function (res) { |
| | | resolve(fileRes); |
| | | }, |
| | | }); |
| | | }, 3000); |
| | | }, |
| | | fail: err => { |
| | | console.log("uni.save--fail"); |
| | | reject(err); |
| | | }, |
| | | }); |
| | | } |
| | | // const isBlob = blobValidate(res.data); |
| | | // if (isBlob) { |
| | | // const blob = new Blob([res.data], { type: "text/plain" }); |
| | | // const url = URL.createObjectURL(blob); |
| | | // const downloadLink = document.getElementById("downloadLink"); |
| | | // downloadLink.href = url; |
| | | // downloadLink.download = file.name; |
| | | // downloadLink.click(); |
| | | // showToast("下载成功"); |
| | | // } else { |
| | | // showToast("下载失败"); |
| | | // } |
| | | }) |
| | | .catch(err => { |
| | | console.error("下载失败:", err); |
| | |
| | | position: fixed; |
| | | bottom: 40rpx; |
| | | right: 40rpx; |
| | | width: 80rpx; |
| | | height: 80rpx; |
| | | width: 130rpx; |
| | | height: 130rpx; |
| | | border-radius: 50%; |
| | | background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); |
| | | display: flex; |