当前位置:网站首页>Fragment upload and breakpoint resume
Fragment upload and breakpoint resume
2022-07-01 05:45:00 【Pengcheng 933】
Fragment upload and breakpoint continuation
Github Warehouse complete code
effect

Use online editing cloudstudio
initialization
- npm i init
- npm i express
- npm i express-fileupload
- npm i axios
- npm i crypto-js
Why does the breakpoint continue
- Browser limits file size
- Retransmit after upload failure
- Interrupt transmission and then transmit again
Realize the idea
front end
- Get the papers , Read the file
function read(file){
const reader=new FileReader()
return new Promise(((resolve, reject) => {
reader.onload=function (){
// Read successfully and return
resolve(reader.result)
}
reader.onerror=reject // Failure to return
reader.readAsBinaryString(file) // Return the read string
}))
}
- MD5 encryption
const hash=CryptoJS.MD5(content)
- Slice the file
while (uploaded<size){
const chunk=file.slice(uploaded,uploaded+chunkSize,type) // file Inherited from blod,blod There are ways to slice, The file can be sliced
const formData=new FormData()
formData.append('name',name)
formData.append('type',type)
formData.append('size',size)
formData.append('hash',hash)
formData.append('file',chunk)
formData.append('offset',uploaded)
formData.append('index',index)
axiosArr.push(formData)
index+=1
uploaded +=chunk.size
}
- Judge whether this file has been uploaded ( That is to say, the upload is suspended in the middle )
const local=localStorage.getItem(hash)
- Continue uploading from the current place
const newDoneArr=[]
isDoneArr.map((item,index)=>{
if(item===0){
newDoneArr.push(axiosArr[index])
}
})
progress.max=newDoneArr.length-1
progress.value=0
multiRequest(newDoneArr,1)
- Start uploading , Join the queue to be uploaded
function enqueue(queue=[],url){
const len=queue.push(url) // Join the queue
request(queue,url) // Request start
return len
}
- The request is successful , Modify state data
const result=await axios.post('/api/upload',formData)
const i=urlsClone.indexOf(formData) // Which file was uploaded
const hash=formData.get('hash') // Get hash identification
localStorage.setItem(hash,i)// Save the uploaded status
result[i]=formData
isDoneArr[i]=1 // Modify the status data to complete
- Determine whether the upload is complete
function dequeue(queue=[],formData){
progress.value += 1 // Where is the current upload
queue.splice(queue.indexOf(formData),1) // from queue Remove completed requests from
if(uls.length){
// If there are still files not uploaded , Continue to upload
enqueue(queue,uls.shift())
}else {
// After uploading, you will be prompted
if(isDoneArr.indexOf(0)===-1){
output.innerText=' Upload successful '
progress.value=urlsClone.length
localStorage.removeItem(formData.get('hash'))
}
}
}
Back end
- utilize FS The module writes data to the file
- Resolve the file name first
const {
name, type, size, offset, hash,index} = req.body
- write file
await appendFile(filename, file.data)
边栏推荐
- HDU - 1024 Max Sum Plus Plus(DP)
- 从MLPerf谈起:如何引领AI加速器的下一波浪潮
- Learn the customization and testing of fpga---ram IP from the bottom structure
- Chapitre d'apprentissage mongodb: Introduction à la première leçon après l'installation
- 输入一个表达式(用字符串表示),求这个表达式的值。
- This is the necessary software for college students 𞓜 knowledge management
- HDU - 1024 Max Sum Plus Plus(DP)
- 2022.6.30-----leetcode. one thousand one hundred and seventy-five
- 【考研高数 武忠祥+880版 自用】高数第二章基础阶段思维导图
- Leetcode top 100 questions 1 Sum of two numbers
猜你喜欢

el-table 动态表头渲染 固定第一列 高度问题

【考研高数 武忠祥+880版 自用】高数第二章基础阶段思维导图

How to transmit and share 4GB large files remotely in real time?

boot+jsp的高校社團管理系統(附源碼下載鏈接)

C语言初阶——牛客网精选好题

HCM 初学 ( 三 ) - 快速输入PA70、PA71 浏览员工信息PA10

穿越派·派盘 + 思源笔记 = 私人笔记本

Crossing pie · pie pan + Mountain duck = local data management

论文学习记录随笔 多标签之LSML

mysql 将毫秒数转为时间字符串
随机推荐
导数的左右极限和左右导数的辨析
Chapitre d'apprentissage mongodb: Introduction à la première leçon après l'installation
从底层结构开始学习FPGA----RAM IP的定制与测试
In win10 and win11, the scroll direction of Elan touch panel is reversed, and "double finger click to open the right-click menu" and "double finger scroll" are started“
扩展点系列之SmartInstantiationAwareBeanPostProcessor确定执行哪一个构造方法 - 第432篇
On the first day of the new year, 3000 Apache servers went down
OneFlow源码解析:算子签名的自动推断
Deeply understand the underlying implementation principle of countdownlatch in concurrent programming
mysql 将毫秒数转为时间字符串
【知识点总结】卡方分布,t分布,F分布
喊我们大学生个人云服务特供商
Vscode function annotation / file header annotation shortcut
College community management system based on boot+jsp (with source code download link)
JDBC常见面试题
论文学习记录随笔 多标签之GLOCAL
Ssgssrcsr differences
boot+jsp的高校社团管理系统(附源码下载链接)
MySQL数据迁移遇到的一些错误
健康照明中应用的LED照明灯
穿越派 你的数据云行