前端form表单+ifarme方式实现大文件下载

javascript 复制代码
// main.js

import Vue from 'vue';
import App from './App.vue';
import { downloadTokenFile } from '@/path/to/your/function'; // 替换为您的函数路径

// 将 downloadTokenFile 添加到 Vue 原型上
Vue.prototype.$downloadTokenFile = downloadTokenFile;

new Vue({
  el: '#app',
  render: h => h(App),
});
javascript 复制代码
// ExampleComponent.vue


export default {
  methods: {
    async downloadFile() {
      try {
        const params = {
          // 下载时的具体参数
                id: row.id,
        };
        await this.$downloadTokenFile('/api/download-url', params);
      } catch (error) {
        console.error(error);
      }
    },
  },
};
/* Ended by AICoder, pid:3998fad1c627467144b30897c0b61912c4b6e505 */
javascript 复制代码
import { getDownloadToken } from '@/api/service';

function createHiddenIframe() {
  const iframe = document.createElement('iframe');
  iframe.name = 'downLoadframe';
  iframe.id = 'hiddenIframe';
  iframe.style.display = 'none';
  document.body.appendChild(iframe);
}

function downloadFile(url, params = {}, authInfo) {
  if (!document.getElementById('hiddenIframe')) {
    createHiddenIframe();
  }

  const iframe = document.getElementById('hiddenIframe');
  const form = document.createElement('form');
  form.method = 'post';
  form.enctype = 'multipart/form-data';
  form.style.display = 'none'; // 隐藏表单

  for (const key in params) {
    const input = document.createElement('input');
    input.type = 'hidden';
    input.name = key;
    input.value = params[key];
    form.appendChild(input);
  }

  document.body.appendChild(form);
  form.action = `${url}?uuid=${authInfo.uuid}&value=${authInfo.value}`;
  form.target = 'downLoadframe';

  form.submit();

  iframe.onload = function () {
    const response = JSON.parse(iframe.contentWindow.document.body.innerText);
    if (response.code !== 0) {
      handleFailure(response.message);
    } else {
      handleSuccess();
    }
  };

  setTimeout(() => {
    document.body.removeChild(form);
  }, 1000); // 等待1秒后移除表单
}

async function downloadTokenFile(url, params) {
  const uuid = new Date().getTime();
  const { data, code } = await getDownloadToken(uuid);

  try {
    if (code === 0) {
      const downloadParams = {
        ...params, //下载时具体参数 对象格式
      };
      const authInfo = {
        //鉴权信息
        value: data,
        uuid,
      };
      downloadFile(url, downloadParams, authInfo);
    } else {
      handleFailure('获取下载令牌失败');
    }
  } catch (error) {
    console.error(error);
    handleFailure('下载文件时发生错误');
  }
}

function handleSuccess() {
  alert('文件下载成功');
}

function handleFailure(message) {
  alert(message);
}

export { downloadTokenFile };
相关推荐
小李子呢02113 小时前
前端八股CSS(2)---动画的实现方式
前端·javascript
GreenTea4 小时前
从 Claw-Code 看 AI 驱动的大型项目开发:2 人 + 10 个自治 Agent 如何产出 48K 行 Rust 代码
前端·人工智能·后端
渣渣xiong5 小时前
从零开始:前端转型AI agent直到就业第五天-第十一天
前端·人工智能
布局呆星5 小时前
Vue3 | 组件通信学习小结
前端·vue.js
C澒5 小时前
IntelliPro 企业级产研协作平台:前端智能生产模块设计与落地
前端·ai编程
OpenTiny社区6 小时前
重磅预告|OpenTiny 亮相 QCon 北京,共话生成式 UI 最新技术思考
前端·开源·ai编程
前端老实人灬6 小时前
web前端面试题
前端
Moment6 小时前
AI 全栈指南:NestJs 中的 Service Provider 和 Module
前端·后端·面试
IT_陈寒7 小时前
为什么我的JavaScript异步回调总是乱序执行?
前端·人工智能·后端