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

#Js篇:两个前端应用通过postMessage传递file对像 URL.createObjectURL+fetch

方案一 通过postMessage传递

A前端发送

const onSendFile = () => {const targetWindow = window.open('http://10.28.105.231:9090');if (targetWindow) {setTimeout(() => {targetWindow.postMessage({ selectedFile: selectedFile.value }, '*');console.log(selectedFile.value);}, 500); // 延迟确保页面加载完成} else {alert('弹出窗口被浏览器拦截,请允许弹出窗口');}
}

B前端接收

    <script>console.log(1111);alert(1111)window.addEventListener('message', (event) => {console.log('event: ', event);console.log(666,event.data); // { id: '123', name: 'Alice' }alert(JSON.stringify(event.data));});</script>

方案二 通过url

A前端发送

 onSendFile() {try {alert(111)const fileURL = URL.createObjectURL(selectedFile.value);console.log(`http://*****:8080#/?file=${encodeURIComponent(fileURL)}`)alert(2222)await dingtalkUtils.navigateToPage(` http://****:8080?file=${encodeURIComponent(fileURL)}&name=${selectedFile.value.name}&type=${selectedFile.value.type}`)} catch (error) {console.log(error)}}

B前端接收

配置跨域

  const params = new URLSearchParams(window.location.search);console.log(params)let file = params.get('file')'let name = params.get('name')let type = params.get('type')let proxy = params.get('proxy')const fileUrl = '/test-api/' + file?.split('/').pop()console.log('fileUrl--999: ', fileUrl);if (file) {// 2. 使用 fetch 获取文件数据//blob:http://10.28.118.4:8080/test-api/979da095-614a-4f2b-98df-3af807645579 fetch(fileUrl).then((response) => response.blob()).then((blob) => {// 3. 重新构造 File 对象(需要知道文件名和类型)const fileName = name as string; // 需要从 A 轻应用传递文件名const fileType = type as string; // 需要从 A 轻应用传递文件类型// 如果 A 轻应用能传递文件名和类型,可以这样:// const fileName = params.get('fileName');// const fileType = params.get('fileType');const newFile = new File([blob], fileName, { type: fileType });console.log('接收到的 File 对象9999:', newFile);console.log('接收到的 File 对象:', newFile.name);console.log('接收到的 File 对象9999:', newFile.type);// 4. 使用 newFile(如上传、预览等)}).catch((error) => {console.error('获取文件失败999:', error);});}

相关文章:

  • Blaster - Multiplayer P117-PXXX: 匹配状态
  • 怒更一波免费声音克隆和AI配音功能
  • qlora
  • MTK平台-- 如何在屏幕关闭时过滤组播和广播的数据包
  • Java开发经验——阿里巴巴编码规范实践解析7
  • 【stm32开发板】原理图设计(电源部分)附:设计PCB流程
  • sql查询中in不生效的问题
  • 【SQL Server Management Studio 连接时遇到的一个错误】
  • 额度年审领域知识讲解
  • 中间表/中转表笔记
  • ⚽【足球数据全维度解析】从基础统计到高阶分析,数据如何重塑现代足球?
  • 用 Python 模拟下雨效果
  • [智能算法]蚁群算法原理与TSP问题示例
  • 软考-系统架构设计师-第二章 嵌入式基础知识
  • 计算机总线技术深度解析:从系统架构到前沿演进
  • 软考-系统架构设计师-第七章 软件工程基础知识
  • MySQL 8.0中的mysql.ibd文件
  • 6个月Python学习计划 Day 8 - Python 函数基础
  • PCIe走线注意事项
  • [250529] CrateDB 5.10.7 发布:一系列重要修复与升级注意事项
  • 口碑好的网站建设公司/大型门户网站建设
  • 标志设计英语/深圳百度seo哪家好
  • java三大框架要学多久/win7优化大师
  • 网站开发者账号购买/郑州网络seo
  • 南昌做网站公司/网络营销论坛
  • 上海网站公安备案/怎么做好网站方式推广