当前位置: 首页 > article >正文

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


// 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),
});
// 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 */
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 };


http://www.kler.cn/news/304496.html

相关文章:

  • OpenAI 刚刚推出 o1 大模型!!突破LLM极限
  • 医疗行业怎么节约和管理能源
  • Leetcode 每日一题:Longest Increasing Path in a Matrix
  • Python安装:Mac 使用brew 安装Python2 和 Python3
  • 古文字定位系统源码分享
  • [项目实战]EOS多节点部署
  • 深入解析 org.apache.maven.plugins
  • 解锁 macOS 剪贴板历史记录,高效复制、粘贴技巧
  • Python 爬虫入门 - 爬虫 requests 请求
  • 内存区域-面试与分析
  • IP网络广播服务平台upload接口存在任意文件上传漏洞
  • Java中等题-移掉k位数字(力扣)
  • Go websocket
  • Python基础学习(1)
  • overleaf如何下载论文的pdf
  • 使用PyTorch进行图像风格迁移:基于VGG19实现
  • Git工作流程
  • Nacos 与 Eureka 的区别
  • vue3 使用 codemirror 实现yaml文件的在线编辑
  • 点餐|基于java的电子点餐系统小程序(源码+数据库+文档)
  • 【Excel 表打印基本操作】
  • 【图像识别】摄像头捕捉运动到静止视频帧(免费源码分享)
  • Maven从入门到精通(三)
  • IVF 视频文件格式
  • [网络][CISCO]CISCO_华为网络设备端口镜像配置
  • Cache Aside pattern
  • EG边缘计算网关连接纵横云3.0物联网平台(MQTT协议)
  • Notepad++插件:TextFX 去除重复行
  • 快速理解Redis
  • 【系统规划与管理师】【案例分析】【考点】【问题篇】第5章 IT服务部署实施