当前位置:网站首页>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)
边栏推荐
猜你喜欢
随机推荐
Ssgssrcsr differences
穿越派·派盘 + 思源笔记 = 私人笔记本
Ucosiii --- engineering transplantation
HCM 初学 ( 二 ) - 信息类型
Floweable source code annotation (40) class delegation
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“
Huluer app help
OpenGL ES: (1) OpenGL ES的由来 (转)
OpenGL ES: (4) EGL API详解 (转)
C语言初阶——实现扫雷游戏
葫芦儿 APP 使用帮助
2022.6.30-----leetcode. one thousand one hundred and seventy-five
从MLPerf谈起:如何引领AI加速器的下一波浪潮
不是你脑子不好用,而是因为你没有找到对的工具
Data governance: data governance framework (Part I)
这才是大学生必备软件 | 知识管理
What things you didn't understand when you were a child and didn't understand until you grew up?
Leetcode top 100 questions 1 Sum of two numbers
Wild melon or split melon?
【考研高数 自用】高数第一章基础阶段思维导图









