前端文件上传方案:别再只用input type=file了

前端文件上传方案:别再只用input type=file了

前端文件上传方案:别再只用input type=file了

毒舌时刻

这代码写得跟网红滤镜似的——仅供参考。

各位前端同行,咱们今天聊聊前端文件上传。别告诉我你还在用原生的input上传大文件,那感觉就像在用小水管灌满游泳池——慢得让人绝望。

为什么你需要文件上传方案

最近看到一个项目,上传100MB的文件直接卡死浏览器,没有任何进度提示,我差点当场去世。我就想问:你是在做上传还是在做浏览器杀手?

反面教材

<!-- 反面教材:原生文件上传 --> <input type="file" onchange="uploadFile(this.files[0])" /> <script> function uploadFile(file) { const formData = new FormData(); formData.append('file', file); // 直接上传,没有进度,没有断点续传 fetch('/api/upload', { method: 'POST', body: formData }); } </script> 

毒舌点评:这代码,我看了都替你的用户着急。原生上传大文件,你是想让用户等到天荒地老吗?

前端文件上传的正确姿势

1. 分片上传

// 正确姿势:分片上传 class ChunkUploader { constructor(file, options = {}) { this.file = file; this.chunkSize = options.chunkSize || 1024 * 1024; // 1MB this.chunks = Math.ceil(file.size / this.chunkSize); this.uploadedChunks = 0; } async upload() { const promises = []; for (let i = 0; i < this.chunks; i++) { const start = i * this.chunkSize; const end = Math.min(start + this.chunkSize, this.file.size); const chunk = this.file.slice(start, end); promises.push(this.uploadChunk(chunk, i)); } await Promise.all(promises); await this.mergeChunks(); } async uploadChunk(chunk, index) { const formData = new FormData(); formData.append('chunk', chunk); formData.append('index', index); formData.append('total', this.chunks); formData.append('filename', this.file.name); await fetch('/api/upload/chunk', { method: 'POST', body: formData }); this.uploadedChunks++; this.onProgress(this.uploadedChunks / this.chunks); } onProgress(progress) { console.log(`上传进度: ${(progress * 100).toFixed(2)}%`); } async mergeChunks() { await fetch('/api/upload/merge', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ filename: this.file.name, chunks: this.chunks }) }); } } // 使用 const uploader = new ChunkUploader(file, { chunkSize: 1024 * 1024 }); uploader.upload(); 

2. 断点续传

// 正确姿势:断点续传 class ResumableUploader { constructor(file) { this.file = file; this.chunkSize = 1024 * 1024; this.uploadedChunks = new Set(); } async init() { // 获取已上传的分片 const response = await fetch(`/api/upload/status?filename=${this.file.name}`); const { uploadedChunks } = await response.json(); this.uploadedChunks = new Set(uploadedChunks); } async upload() { const chunks = Math.ceil(this.file.size / this.chunkSize); for (let i = 0; i < chunks; i++) { if (this.uploadedChunks.has(i)) { console.log(`分片${i}已上传,跳过`); continue; } const start = i * this.chunkSize; const end = Math.min(start + this.chunkSize, this.file.size); const chunk = this.file.slice(start, end); await this.uploadChunk(chunk, i); this.uploadedChunks.add(i); // 保存进度到本地 localStorage.setItem('uploadProgress', JSON.stringify([...this.uploadedChunks])); } await this.mergeChunks(); localStorage.removeItem('uploadProgress'); } async uploadChunk(chunk, index) { // 上传逻辑... } } 

3. 拖拽上传

// 正确姿势:拖拽上传 import { useCallback } from 'react'; function DragUpload({ onUpload }) { const handleDrop = useCallback((e) => { e.preventDefault(); const files = Array.from(e.dataTransfer.files); files.forEach(file => onUpload(file)); }, [onUpload]); const handleDragOver = useCallback((e) => { e.preventDefault(); }, []); return ( <div className="drag-upload" onDrop={handleDrop} onDragOver={handleDragOver} > <p>拖拽文件到此处上传</p> <input type="file" multiple onChange={(e) => { Array.from(e.target.files).forEach(file => onUpload(file)); }} /> </div> ); } 

毒舌点评:早这么写,你的上传早就做好了。别告诉我你还在用原生input,那你还是趁早去用FTP吧。

实战技巧:文件上传指南

1. 上传优化策略

  1. 分片上传:大文件切分上传
  2. 断点续传:支持暂停恢复
  3. 并发控制:限制同时上传数量
  4. 进度显示:实时显示上传进度

2. 最佳实践

// ✅ 显示上传进度 const xhr = new XMLHttpRequest(); xhr.upload.onprogress = (e) => { const progress = (e.loaded / e.total) * 100; console.log(`${progress}%`); }; // ✅ 图片预览 const preview = URL.createObjectURL(file); // ✅ 文件类型检查 const allowedTypes = ['image/jpeg', 'image/png']; if (!allowedTypes.includes(file.type)) { alert('不支持的文件类型'); return; } 

最后想说的

文件上传不是小事,是用户体验的关键。别再只用input type=file了——优化一下,你的上传会更专业。

文件上传就像快递,原生input像平邮,优化后的上传像顺丰。别让用户等平邮,给他们顺丰的体验。

Read more

彻底解决 ComfyUI Mixlab 插件 Whisper.available False 的报错

彻底解决 ComfyUI Mixlab 插件 Whisper.available False 的报错

https://github.com/MixLabPro/comfyui-mixlab-nodes 彻底解决 ComfyUI Mixlab 插件 Whisper.available False 的报错 在 ComfyUI 中安装 Mixlab Nodes 插件后,控制台显示其他节点正常,便 Whisper.available False。即使环境里安装了 openai-whisper 和 faster-whisper,问题依然可能存在。 Whisper.available False 本文将分享如何通过修改 __init__.py 进行深度 Debug,并修复 Whisper.py 中的路径逻辑漏洞。 1. 深度排查:让报错“开口说话” Mixlab 的默认日志只提示 False,不显示原因。为了抓出真凶,

【GitHub项目推荐--TypeTale(字字动画):免费AIGC视频创作工具】非开源

简介 TypeTale (字字动画)是一款专为内容创作者打造的完全免费的AIGC创作软件,主要用于小说推文、AI短剧、AI电影制作。它集成了多种AI能力,提供从文案处理到视频生成的全链路创作支持,承诺现有功能与基础功能永久免费。 🔗 GitHub地址 : https://github.com/TypeTale/TypeTale 🎬 核心价值 : AIGC视频生成 · 小说推文 · AI短剧 · 完全免费 · 中文优化 项目背景 : * 内容创作 :短视频内容创作需求增长 * AIGC技术 :AI生成内容技术成熟 * 成本控制 :降低视频制作成本需求 * 中文优化 :中文内容创作工具需求 * 开源生态 :开源创作工具生态 项目特色 : * 🆓 完全免费 :永久免费使用 * 🇨🇳 中文优化 :专为中文优化 * 🤖 AI集成 :多AI能力集成 * 🎬 视频生成 :全链路视频生成 * 🔧 易用性 :简单易用界面 技术亮点 : * 多模型支持 :支持多种AI模型 * ComfyUI集成 :深度ComfyUI集成 * 工作流系统

群消息机器人统计分析

群消息机器人统计分析

文章目录 * 一、企业微信群机器人 + 腾讯云函数 * 1.1 注册企业微信 * 1.2 注册腾讯云 * 1.3 创建群聊并添加机器人 * 1.4 创建腾讯云函数 * 二、个人微信(封号风险) * 3.1 itchat * 3.2 Wechaty * 3.3 ChatLog * 3.4 群洞察小程序 * 3.5 koishi * 三、钉钉开发平台 * 3.1 使用手册 * 3.2 钉钉机器人API * 3.3 Python脚本实现Send_message * **** 权限隔离 vs. 会话存档: * **** @机器人触发统计

服务器环境 VsCode:Github Copilot 安装完成却用不了?关键步骤补全

GitHub Copilot在VS Code中无法使用的关键解决步骤 1. 基础环境检查 * VS Code版本:确保使用最新版(至少≥1.60),旧版可能导致兼容问题 * Copilot状态:在VS Code左侧活动栏点击Copilot图标(飞机形状),检查是否显示已登录和启用状态 * 网络环境:Copilot需访问GitHub服务器,尝试关闭代理或检查防火墙是否屏蔽api.github.com 2. 核心配置步骤 # 步骤1:检查Copilot是否激活 # 在VS Code命令面板(Ctrl+Shift+P)输入: > GitHub Copilot: Check Status # 步骤2:重置授权令牌(常见问题根源) > GitHub Copilot: Reset GitHub Copilot Token # 步骤3:强制刷新扩展 >