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

Uniapp实现多种文件类型上传

一、前言

在移动端开发中,文件上传是常见的功能需求。本文将通过Uniapp框架,详细讲解如何实现支持多类型文件(图片、视频、文档等)的上传功能,并解决跨平台兼容性问题😄😄😄。


二、技术方案

2.1 核心API

Uniapp提供了以下关键API:

  • uni.chooseFile:文件选择
  • uni.uploadFile:文件上传
  • uni.getFileInfo:获取文件信息

2.2 平台差异处理

平台文件选择方式限制说明
H5<input type="file">依赖浏览器实现
微信小程序wx.chooseMessageFile需配置合法域名
Appplus.io 文件系统需处理本地文件路径

三、完整实现代码

3.1 文件选择器封装

// 多类型文件选择
function chooseFiles(fileType = 'all') {
  return new Promise((resolve, reject) => {
    const extnameMap = {
      image: ['png', 'jpg', 'jpeg'],
      video: ['mp4', 'mov'],
      document: ['pdf', 'doc', 'docx', 'xls']
    };

    uni.chooseFile({
      count: 5, // 最大选择数量
      extension: fileType === 'all' ? [] : extnameMap[fileType],
      success: res => {
        const files = res.tempFiles.map(item => ({
          path: item.path,
          name: item.name,
          size: item.size,
          type: item.type
        }));
        resolve(files);
      },
      fail: err => reject(err)
    });
  });
}

3.2 文件上传核心方法

// 上传文件到服务器
async function uploadFile(file) {
  try {
    const formData = {
      userId: '123',
      fileType: file.type
    };

    const res = await uni.uploadFile({
      url: 'https://api.example.com/upload',
      filePath: file.path,
      name: 'file',
      formData,
      header: {
        'Authorization': 'Bearer token'
      }
    });

    return JSON.parse(res[1].data);
  } catch (error) {
    console.error('上传失败:', error);
    throw error;
  }
}

3.3 进度显示实现

// 带进度上传
function uploadWithProgress(file, onProgress) {
  return new Promise((resolve, reject) => {
    const task = uni.uploadFile({
      url: 'https://api.example.com/upload',
      filePath: file.path,
      name: 'file',
      success: (res) => resolve(JSON.parse(res.data)),
      fail: reject,
      complete: () => task.offProgressUpdate()
    });

    task.onProgressUpdate((res) => {
      onProgress && onProgress({
        progress: res.progress,
        totalBytesSent: res.totalBytesSent,
        totalBytesExpectedToSend: res.totalBytesExpectedToSend
      });
    });
  });
}

四、界面实现示例

<template>
  <view class="upload-container">
    <button @click="chooseFiles">选择文件</button>
    <view class="preview-list">
      <view v-for="(file, index) in files" :key="index" class="file-item">
        <image v-if="file.type.startsWith('image/')" :src="file.path" mode="aspectFit"/>
        <video v-else-if="file.type.startsWith('video/')" :src="file.path"/>
        <view v-else class="document-icon">
          <text>{{ getFileExt(file.name) }}</text>
        </view>
        <progress :percent="file.progress" show-info />
      </view>
    </view>
  </view>
</template>

<style>
/* 文件预览样式 */
.preview-list {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-top: 20px;
}

.file-item {
  position: relative;
  width: 100px;
  height: 100px;
  border: 1px dashed #ddd;
}

.document-icon {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background: #f0f0f0;
}
</style>

五、服务端配合建议

5.1 文件接收配置(Node.js示例)

const multer = require('multer');
const storage = multer.diskStorage({
  destination: 'uploads/',
  filename: (req, file, cb) => {
    const ext = path.extname(file.originalname);
    cb(null, `${Date.now()}${ext}`);
  }
});

const upload = multer({
  storage,
  limits: {
    fileSize: 1024 * 1024 * 50 // 50MB
  },
  fileFilter: (req, file, cb) => {
    const allowedTypes = ['image/jpeg', 'image/png', 'video/mp4', 'application/pdf'];
    cb(null, allowedTypes.includes(file.mimetype));
  }
});

router.post('/upload', upload.single('file'), (req, res) => {
  // 处理上传成功逻辑
});

六、注意事项🐛

  1. 文件大小限制:需同时在前端和服务端设置
  2. 格式验证:不能仅依赖前端验证
  3. 安全处理
    • 重命名存储文件
    • 扫描恶意文件
    • 设置访问权限
  4. 性能优化
    • 图片压缩(可使用uni.compressImage)
    • 分片上传大文件
    • 断点续传

http://www.kler.cn/a/580284.html

相关文章:

  • 串口数据记录仪DIY,体积小,全开源
  • 代码随想录刷题day40|(二叉树篇)101.对称二叉树
  • langchain4j+PDFBox小试牛刀
  • flink cdc同步mysql数据
  • deepseek在pycharm中的配置和简单应用
  • 科技的成就(六十七)
  • 深度学习五大模型全解析:CNN、Transformer、BERT、RNN、GAN 的区别与联系,一文读懂!
  • osgEarth 加载MapBox的pbf瓦片数据
  • 控制--机器人模型--四旋翼无人机
  • 深入探索 Dubbo:高效的 Java RPC 框架
  • 化工厂防爆气象站:为石油化工、天然气等领域提供安全保障
  • (每日一题) 力扣 14 最长公共前缀
  • GreatSQL 8.0.32-27 GA (2025-3-10)
  • 几种linux获取系统运行时间的方法
  • java中泛型
  • Mysql主从复制和Mysql高可用以及负载均衡配置
  • 解锁下一代开发范式:IntelliJ Idea AI插件全景实战与未来展望
  • Python数据可视化自动化工具:让数据跃然纸上
  • King3399(ubuntu文件系统)Qt(C++)串口工具开发
  • 掌握Excel快捷键与函数公式,开启高效办公之旅