12345678910111213141516171819202122232425 |
- // 在你的 Vue 组件中引入 axios 或者其他网络请求库
- import axios from "axios";
- const downService = axios.create({
- baseURL: import.meta.env.VITE_APP_UPLOAD_URL,
- });
- async function downloadFile(url: string, fileName: string) {
- try {
- const response = await downService.get(url, {
- responseType: "blob",
- });
- const blob = new Blob([response.data]);
- const link = document.createElement("a");
- const resurl = url;
- link.href = window.URL.createObjectURL(blob);
- const lastDotIndex = resurl.lastIndexOf(".");
- link.download = fileName + "." + resurl.substring(lastDotIndex + 1);
- document.body.appendChild(link);
- link.click();
- window.URL.revokeObjectURL(link.href);
- document.body.removeChild(link);
- } catch (error) {
- console.error("文件下载失败:", error);
- }
- }
- export { downloadFile };
|