终极免费方案:如何5分钟搞定Axure RP全界面中文汉化
2026/5/24 14:24:17
开发日记:大文件上传系统攻坚记
日期:2023年XX月XX日
天气:晴(但心情像在调试IE8的阴天)
客户的需求像一座高山:
XDomainRequest和ActiveXObject的祖传代码)FileSystemEntryAPI)技术选型:
- 放弃WebUploader(IE8支持不足),选择原生JS+H5 File API
- 加密方案:SM4/AES通过
forge.js兼容旧浏览器- 断点续传:
localStorage+服务端分片记录
// 获取文件夹所有文件(兼容IE10+,IE8需ActiveX降级方案)asyncfunctionscanDirectory(directory){constfiles=[];conststack=[directory];while(stack.length){constentry=stack.pop();if(entry.isFile){files.push(awaitgetFile(entry));}elseif(entry.isDirectory){constreader=entry.createReader();constentries=awaitnewPromise(resolve=>reader.readEntries(resolve));stack.push(...entries);}}returnfiles;}// IE8降级方案(需用户手动选择多个文件)functionhandleIE8Fallback(){constinput=document.createElement('input');input.type='file';input.multiple=true;input.onchange=()=>uploadFiles(input.files);input.click();}classChunkUploader{constructor(file,options){this.file=file;this.chunkSize=5*1024*1024;// 5MB/chunkthis.totalChunks=Math.ceil(file.size/this.chunkSize);this.uploadedChunks=JSON.parse(localStorage.getItem(file.name)||'[]');}asyncupload(){for(leti=0;i<this.totalChunks;i++){if(this.uploadedChunks.includes(i))continue;constchunk=this.file.slice(i*this.chunkSize,(i+1)*this.chunkSize);awaitthis.sendChunk(chunk,i);this.uploadedChunks.push(i);localStorage.setItem(this.file.name,JSON.stringify(this.uploadedChunks));}}asyncsendChunk(chunk,index){constformData=newFormData();formData.append('chunk',chunk);formData.append('chunkIndex',index);formData.append('totalChunks',this.totalChunks);formData.append('fileId',btoa(this.file.name+this.file.size));// 唯一标识returnfetch('/api/upload',{method:'POST',body:formData});}}[WebMethod]publicstaticstringUploadChunk(){HttpPostedFilechunk=HttpContext.Current.Request.Files["chunk"];intindex=int.Parse(HttpContext.Current.Request["chunkIndex"]);stringfileId=HttpContext.Current.Request["fileId"];// 存储分片到临时目录stringtempPath=Path.Combine(Server.MapPath("~/App_Data/Temp"),fileId);Directory.CreateDirectory(tempPath);chunk.SaveAs(Path.Combine(tempPath,index.ToString()));// 检查是否完成if(AllChunksReceived(tempPath,int.Parse(HttpContext.Current.Request["totalChunks"]))){MergeFile(tempPath,fileId);}return"OK";}publicstaticvoidEncryptToOSS(Streaminput,stringossKey){varengine=newSM4Engine();varkey=Encoding.UTF8.GetBytes("16-byte-secret-key");engine.Init(true,newKeyParameter(key));using(varossClient=newOssClient(endpoint,accessKeyId,accessKeySecret))using(varcryptoStream=newCryptoStream(input,engine,CryptoStreamMode.Read)){ossClient.PutObject(bucketName,ossKey,cryptoStream);}}swfUpload后备方案FileReader的readAsArrayBuffer分段处理/科研资料/2023/实验报告.pdfCREATETABLEFileTransfers(FileIdVARCHAR(255)PRIMARYKEY,OriginalName NVARCHAR(255),OSSPathVARCHAR(255),ChunkCountINT,CompletedChunksVARCHAR(MAX)-- JSON格式存储已上传分片)明日计划:
签名:
—— 一个在古董浏览器和现代需求间挣扎的开发者
安装.NET Framework 4.7.2
https://dotnet.microsoft.com/en-us/download/dotnet-framework/net472
框架选择4.7.2
NOSQL无需任何配置可直接访问页面进行测试
使用IIS
大文件上传测试推荐使用IIS以获取更高性能。
小文件上传测试可以使用IIS Express
相关参考:
文件保存位置,
支持离线保存文件进度,在关闭浏览器,刷新浏览器后进行不丢失,仍然能够继续上传
支持上传文件夹并保留层级结构,同样支持进度信息离线保存,刷新页面,关闭页面,重启系统不丢失上传进度。
支持文件批量下载
文件下载支持离线保存进度信息,刷新页面,关闭页面,重启系统均不会丢失进度信息。
支持下载文件夹,并保留层级结构,不打包,不占用服务器资源。
下载完整示例