[Design Pattern] Upload big file - 2. How to coordinate frontend and backend
How to coordinate frontend and backend?
File upload involves interaction between the frontend and backend, requiring the establishment of a standard communication protocol to accomplish the following core interactions:
- File protocol creation
- Hash verification
- Chunked data upload
- Chunk merging
File protocol creation
When the client sends a chunk to the server, it needs to inform the server which file upload the chunk belongs to. Therefore, a unique identifier is required to distinguish a specific file upload.
The file creation protocol is designed to generate and retrieve this unique identifier for the file upload process.
From Client side, it sends following information to BFF
// GET request
Upload-File-Size
Upload-File-Name
Upload-File-MIME
BFF need to tell Frontend:
status
uploadToken
chunkSize
- uploadToken: A unique identifier for the file upload process. Can be generated by
uuid
- chunksize: The size of each chunk in bytes.
Hash verification
The client may need to verify the hash of a single chunk or the entire file. The server should provide the client with the current status of these hashes.
Frontend need to send:
Upload-Token
Upload-Hash-Type: chunk | file
Upload-Hash
BFF need to tell:
status
hasFile
hasFile
: information is important, ifhasFile
, then can skip that chunk upload
For the whole file, BFF also need to response:
rest: [[], []] // each inner array represent a chunk [from, end]
url
Chunked data upload
Upload specific file chunk data through this protocol.
About the Content-Type
:
1. For General File Uploads
multipart/form-data
: Used for uploading files along with other form fields in a single request. This is the most common content type for file uploads in forms.application/octet-stream
: Used for binary file uploads where the file is sent directly in the request body without any additional encoding.2. For Specific File Types
text/plain
: For plain text files.application/json
: If the file content is JSON or metadata is included as JSON.image/png
,image/jpeg
,image/gif
: For image files.application/pdf
: For PDF files.application/zip
,application/x-tar
: For compressed files.3. For Chunked Uploads
application/octet-stream
: Commonly used for uploading binary chunks.- Custom Content-Type: Some systems may define custom
Content-Type
values, such asapplication/vnd.upload-chunk
or similar, depending on implementation.
Chunk merging
After all chunks have been uploaded, use this protocol to request the server to complete chunk merging.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2022-12-05 [Typescript] 127. Hard - Assign
2019-12-05 [Algorithm] 94. Binary Tree Inorder Traversal iteratively approach
2018-12-05 [Cypress] install, configure, and script Cypress for JavaScript web applications -- part2
2018-12-05 [Cypress] install, configure, and script Cypress for JavaScript web applications -- part1
2016-12-05 [Angular2 Router] Get activated router url
2014-12-05 [Express] Level 5: Route file