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

uniapp 使用 XMLHttpRequest发送二进制上传文件 可使用预上传接口上传到华为obs

<template> <button @click="changeMessage">选择文件</button> <view v-show="false" :path="path" :change:path="requestModule.uploadOBS"></view> </template> <script setup> import { ref } from 'vue' import { getCurrentInstance } from 'vue' const { proxy } = getCurrentInstance() // 把回调挂到实例上,renderjs 就能通过 callMethod 调到 proxy.$onRenderData = (data) => { console.error('str:', data) } const path = ref(null) const changeMessage = () => { uni.chooseImage({ count: 1, // 最多选择数量 sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图 sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机 success: async (res) => { // tempFilePath 可以作为 img 标签的 src 属性显示图片 const tempFilePaths = res.tempFilePaths path.value = tempFilePaths[0]; }, fail: (err) => { console.error('选择图片失败:', err) } }) } </script> <script> // 专门给 renderjs 回调用 ownerVm.callMethod('onRenderData', "999999") 就能调到 /* export default { methods: { onRenderData(data) { console.error('str:', data) } } } */ </script> <script module="requestModule" lang="renderjs"> export default { methods: { /* 1. 逻辑层 → renderjs */ uploadOBS(newVal, oldVal, ownerVm, vm) { console.error('path:', newVal) this.getFileAsArrayBuffer2(newVal).then(res => { console.error('arrayBuffer:', res) return this.uploadArrayBuffer(res,'https://jszj-dev.obs.cn-east-3.myhuaweicloud.com/2011725287372374017/jszj_lp_merchant_apply_for_application_materials_file_id/20260116/1762482093185_1768566856106.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20260116T123416Z&X-Amz-SignedHeaders=host&X-Amz-Credential=HPUAACHPJ15SJKPZGOAP%2F20260116%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Expires=86400&X-Amz-Signature=29eb09d67a7bf73afb199e75148c57d73d13d65146fe6bd351b479cc0ebd66d1','image/jpeg'); }).then(res => { console.error('upload:', res) }).catch(err => { }) //调用vue2方法 //ownerVm.callMethod('onRenderData', "999999") //调用vue3方法 ownerVm.callMethod('$onRenderData', "999999") }, getFileAsArrayBuffer2(filePath) { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest() xhr.open('GET', filePath, true) xhr.responseType = 'arraybuffer' xhr.onload = function() { if (xhr.status === 200) { resolve(xhr.response) } else { reject(new Error('Failed to load file')) } } xhr.onerror = function() { reject(new Error('Network error')) } xhr.send() }) }, uploadArrayBuffer(arrayBuffer, uploadUrl, fileType = 'application/octet-stream') { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); /* 1. 打开连接 */ xhr.open('PUT', uploadUrl, true); /* 2. 设置请求头 */ xhr.setRequestHeader('Content-Type', fileType); /* 3. 响应类型(按需要) */ xhr.responseType = 'arraybuffer'; /* 4. 上传进度 */ xhr.upload.onprogress = function(e) { if (e.lengthComputable) { const percent = (e.loaded / e.total) * 100; console.log('上传进度:', percent.toFixed(2) + '%'); } }; /* 5. 成功/失败 */ xhr.onload = function() { if (xhr.status >= 200 && xhr.status < 300) { resolve(xhr.response); // 服务端返回的 ArrayBuffer } else { reject(new Error(`上传失败 ${xhr.status}`)); } }; xhr.onerror = () => reject(new Error('网络错误')); /* 6. 发送二进制 */ xhr.send(arrayBuffer); }); } } } </script> <style> </style>
http://www.jsqmd.com/news/257299/

相关文章:

  • Hunyuan开源模型值不值得用?技术架构深度解析指南
  • BERT智能填空避坑指南:常见问题与解决方案汇总
  • PDF-Extract-Kit版面分析:复杂文档结构识别技巧
  • 实测通义千问2.5-7B-Instruct:AI对话效果惊艳,附完整部署教程
  • Ffmpeg.js 终极指南:浏览器端音视频处理的完整解决方案
  • 从文本向量化到聚类优化|GTE大模型镜像应用全链路
  • uni.chooseMedia 返回 /storage/emulated/ 开头或 content://media/external/开头
  • LinkSwift终极网盘直链下载助手完整使用教程
  • 小白必看:通义千问2.5-7B-Instruct快速入门与API调用指南
  • YOLO26模型融合:Ensemble推理性能提升
  • Windows HEIC缩略图终极方案:让苹果照片在资源管理器完美预览
  • 从架构到部署:AutoGLM-Phone-9B实现手机端低延迟多模态推理
  • 腾讯优图Youtu-2B案例:金融行业智能助手实现
  • PDF智能提取全攻略|基于PDF-Extract-Kit镜像快速实现布局与公式识别
  • 通义千问2.5-7B-Instruct工具调用实战:Agent接入详细步骤
  • [C++][cmake]基于C++在windows上使用纯opencv部署yolo26的图像分类onnx模型
  • 边疆政务翻译难题破局|HY-MT1.5-7B模型镜像实测与合规性探讨
  • AI读脸术多任务并行优势:单次推理完成三项检测
  • AI智能二维码工坊实操手册:从零搭建本地化解码服务
  • Obsidian手写笔记终极指南:数字笔记与自然书写的完美融合
  • 5个开源大模型部署教程:NewBie-image-Exp0.1免配置环境一键启动实测
  • 智能小车PCB板原理图从零实现教程
  • Sambert语音合成教程:构建支持RESTful API的服务
  • 核心要点解析:ESP32运行轻量级音频分类模型的方法
  • Qwen All-in-One保姆级教程:无需GPU的极速部署方案
  • 高效网盘下载助手完整配置与使用教程
  • 互联网大厂Java面试实战:涵盖Spring Boot、微服务与AI应用技术
  • 终极指南:八大网盘直链解析神器,告别下载限速烦恼
  • 一键部署NewBie-image-Exp0.1:3.5B大模型开箱即用指南
  • DeepSeek-R1知识库应用:私有数据安全方案,免开发部署