当前位置:网站首页>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)
边栏推荐
- JDBC common interview questions
- C语言初阶——牛客网精选好题
- 移动端常用解决方案
- 4GB大文件,如何实时远程传输和共享?
- OpenGL ES: (3) EGL、EGL绘图的基本步骤、EGLSurface、ANativeWindow
- Daily code 300 lines learning notes day 11
- 数据治理:数据治理管理(第五篇)
- HCM 初学 ( 二 ) - 信息类型
- 2022第八届中国国际“互联网+”大学生创新创业大赛产业命题赛道开启报名!
- [ffmpeg] [reprint] image mosaic: picture in picture with wheat
猜你喜欢
随机推荐
Basic electrician knowledge 100 questions
不是你脑子不好用,而是因为你没有找到对的工具
2022第八届中国国际“互联网+”大学生创新创业大赛产业命题赛道开启报名!
mysql 将毫秒数转为时间字符串
这才是大学生必备软件 | 知识管理
码蹄集 - MT3149 · AND - 数据不是很强,暴力剪枝就能骗AC
Know the future of "edge computing" from the Nobel prize!
[medical segmentation] u2net
Summary of common components of applet
idea启动查看项目端口
我从技术到产品经理的几点体会
In depth understanding of condition source code interpretation and analysis of concurrent programming
【考研高数 武忠祥+880版 自用】高数第二章基础阶段思维导图
Data governance: metadata management implementation (Part IV)
数据治理:元数据管理实施(第四篇)
【考研高数 自用】高数第一章基础阶段思维导图
[SRS] use of Vhost isolated stream: push / pull Stream Address
云盘里资料被和谐了,怎么办?
导数的左右极限和左右导数的辨析
MySQL数据迁移遇到的一些错误








![[medical segmentation] u2net](/img/b3/b1d188216310fe1217e360ac56af3b.jpg)
