search
HomeWeb Front-endVue.jsA brief analysis of how vue implements file slicing upload

A brief analysis of how vue implements file slicing upload

Mar 24, 2023 pm 07:40 PM
vue.jsFile Uploadfile slice

A brief analysis of how vue implements file slicing upload

In the process of actual development projects, sometimes it is necessary to upload relatively large files. Then, the upload will be relatively slow, so the background may require the front-end to process the files. Slice upload is very simple. It is to cut a file stream of, for example, 1 G into several small file streams, and then request the interface to deliver these small file streams respectively.

Process Brief

To implement file slice import, first we use elementUI or the native upload tag to obtain the file file stream, and then we need to do One thing is to know whether this file has been uploaded before. We need to submit a unique identifier of this file to the backend, and then let the backend tell us whether the problem exists in the backend. At this time, the backend may return us three statuses :

  • There are no files, all need to be uploaded.

  • This file exists and the front end does not need to upload it again.

  • Part of this file has been uploaded, and you need to continue uploading the parts that have not been uploaded.

There are only these three states. An important parameter to implement this step is the unique identifier of the file. This identifier uses the MD5 identification code of the file. So we first need to get the MD5 unique identification code of this file. [Related recommendations: vuejs video tutorial, web front-end development]

We know that when a file needs to be uploaded or needs to be partially uploaded, we need to perform a slicing operation . This is very simple. For example, the binary byte stream of a file is 1 G in total, and then it is usually cut into one piece of 5M. So if it is a 1G file, it needs to be cut into 205 pieces. Of course, the last piece is not necessarily 5M, and then we go The interface for uploading files in parts, initiating requests to import files into parts.

First of all, let’s make it clear that after we submit these 205 fragmented data streams, the background needs to be merged, that is, the 205 file streams submitted by our fragments are synthesized into a large file and saved. In this way, the files merged in the background are the files we will eventually submit for import. Since the backend needs to be merged, we upload them piece by piece. We have to let the backend know which part of the file each piece is, right? So when we submit a sharding request, we generally need to pass these parameters:

  • chunk: The current shard number, usually starting from 0.
  • chunkSize: The size of the fragment, usually 5M, which is 5242880 bytes.
  • chunks: How many shards are there in total.
  • file: The file stream of the current fragment.
  • md5: MD5 unique identification code of the entire file, not fragmented.
  • name: The name of the current file.
  • size: Current fragment size (if the last block is not necessarily 5M).

This way the backend knows how to merge the final files. Of course, specific fields need to be defined by the backend, and then we can connect accordingly according to this idea.

OK, after all our 205 shard data are submitted, we need to go through another interface, which is to notify the backend. Okay, my front-end shards have been transmitted, and you can merge the files.

Then the backend merge is completed and the file is imported successfully!

Get the MD5 unique identification code of the file

Many people say that MD5 is not encryption. In fact, this idea is not very correct here. MD5 cannot encrypt files. Yes, it will only generate a unique code. So, oh, just think of it as encryption, but don’t think that submitting an MD5 code is just like submitting an encrypted file to the backend. First of all, this is a string, not Encrypted files, secondly, this thing cannot decrypt. We just generate a unique identifier for the file here, so that the backend can determine whether the file has existed on the server before. If there is a unique identifier, it means it has been uploaded before. You can just use the previous one without uploading it again. , after all, if the file is modified, the MD5 identification code will change.

How to get the MD5 encoding of a file is very simple. You need to use a library spark-md5 in vue.

One command to install

npm install --save spark-md5

Then we can write a method to encapsulate it.

Create a fileMd5Sum.js file:

import SparkMD5 from 'spark-md5'

export default {
  // md5值计算
  fileMd5Sum(file) {
    return new Promise(resolve => {
      let fileReader = new FileReader()
      let Spark = new SparkMD5.ArrayBuffer()
      fileReader.readAsArrayBuffer(file)
      fileReader.onload = function (e) {
        Spark.append(e.target.result)
        let md5 = Spark.end()
        resolve(md5)
      }
    });
  }
}

Then you can use it when you need to use it. Of course, what is returned here is a Promise, just .then Got it.

Or use async, await.

let md5Str = await this.fileMd5Sum.fileMd5Sum(file.raw)

文件切片

获取了文件MD5标识码,后台说需要提交,我们就需要把文件切片,从头提交或者是提交部分操作了,如果不需要的话直接走合并接口就可以了,走合并接口其实是告诉后台合并,我们要做的就是把其他除了文件相关的其他参数传递给后台罢了。

文件切片就是类似于字符串截取,这里是截取字节。获取需要的参数我们自己些就可以了。假设我们使用 elementUI 文件上传组件获取到了文件 file

获取文件名 name

let fileName = file.name  // 获取文件名

分片文件大小 chunkSize

let chunkSize = 5 * 1024 * 1024   // 一般是 5M,具体多少看后端想要多少

文件切片 chunkList 列表

            let chunkList = []  // 创建一个数组用来存储每一片文件流数据
            if (file.size < chunkSize) {  // 如果文件大小小于5M就只有一片,不用切都
              chunkList.push(file.raw.slice(0))  // 文件流从第一个字节直接截取到最后就可以了
            } else {  // 如果文件大小大于5M 就需要切片了
              var start = 0, end = 0  // 创建两个变量 开始位置 结束位置
              while (true) {  // 循环
                end += chunkSize  // 结束为止 = 结束位置 + 每片大小
                let blob = file.raw.slice(start, end)  // 文件流从开始位置截取到结束位置
                start += chunkSize  // 截取完,开始位置后移
                if (!blob.size) {  // 如果截取不到了就退出
                  break;
                }
                chunkList.push(blob)  // 把截取的每一片数据保存到数组
              }
            }

切片总数 chunks

我们上一步已经获取到每片文件流的数组了,所以说呢,直接获取就可以了。

let chunks = chunkList.length

切片大小  size

我们是按照 5 M 切割的,所以说每片大小应该是 5 * 1024 * 1024   但是呢,不对,因为最后一片不一定就是正好的 5 M,所以说我们可直接 .size 获取一下大小。比如:

chunkList[0].size  // 获取第1片大小

参数都找齐了,然后就走切片提交接口开始提交数据就可以了。

合并

当我们把分片数据全部提交成功,后台返回说切片文件保存成功之后,我们就可以走最后一个接口,提交就可以了。

好了,就这样!完成!!!

(学习视频分享:vuejs入门教程编程基础视频

The above is the detailed content of A brief analysis of how vue implements file slicing upload. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete
What Happens When the Vue.js Virtual DOM Detects a Change?What Happens When the Vue.js Virtual DOM Detects a Change?May 14, 2025 am 12:12 AM

WhentheVue.jsVirtualDOMdetectsachange,itupdatestheVirtualDOM,diffsit,andappliesminimalchangestotherealDOM.ThisprocessensureshighperformancebyavoidingunnecessaryDOMmanipulations.

How Accurate Is It to Think of Vue.js's Virtual DOM as a Mirror of the Real DOM?How Accurate Is It to Think of Vue.js's Virtual DOM as a Mirror of the Real DOM?May 13, 2025 pm 04:05 PM

Vue.js' VirtualDOM is both a mirror of the real DOM, and not exactly. 1. Create and update: Vue.js creates a VirtualDOM tree based on component definitions, and updates VirtualDOM first when the state changes. 2. Differences and patching: Comparison of old and new VirtualDOMs through diff operations, and apply only the minimum changes to the real DOM. 3. Efficiency: VirtualDOM allows batch updates, reduces direct DOM operations, and optimizes the rendering process. VirtualDOM is a strategic tool for Vue.js to optimize UI updates.

Vue.js vs. React: Scalability and MaintainabilityVue.js vs. React: Scalability and MaintainabilityMay 10, 2025 am 12:24 AM

Vue.js and React each have their own advantages in scalability and maintainability. 1) Vue.js is easy to use and is suitable for small projects. The Composition API improves the maintainability of large projects. 2) React is suitable for large and complex projects, with Hooks and virtual DOM improving performance and maintainability, but the learning curve is steeper.

The Future of Vue.js and React: Trends and PredictionsThe Future of Vue.js and React: Trends and PredictionsMay 09, 2025 am 12:12 AM

The future trends and forecasts of Vue.js and React are: 1) Vue.js will be widely used in enterprise-level applications and have made breakthroughs in server-side rendering and static site generation; 2) React will innovate in server components and data acquisition, and further optimize the concurrency model.

Netflix's Frontend: A Deep Dive into Its Technology StackNetflix's Frontend: A Deep Dive into Its Technology StackMay 08, 2025 am 12:11 AM

Netflix's front-end technology stack is mainly based on React and Redux. 1.React is used to build high-performance single-page applications, and improves code reusability and maintenance through component development. 2. Redux is used for state management to ensure that state changes are predictable and traceable. 3. The toolchain includes Webpack, Babel, Jest and Enzyme to ensure code quality and performance. 4. Performance optimization is achieved through code segmentation, lazy loading and server-side rendering to improve user experience.

Vue.js and the Frontend: Building Interactive User InterfacesVue.js and the Frontend: Building Interactive User InterfacesMay 06, 2025 am 12:02 AM

Vue.js is a progressive framework suitable for building highly interactive user interfaces. Its core functions include responsive systems, component development and routing management. 1) The responsive system realizes data monitoring through Object.defineProperty or Proxy, and automatically updates the interface. 2) Component development allows the interface to be split into reusable modules. 3) VueRouter supports single-page applications to improve user experience.

What are the disadvantages of VueJs?What are the disadvantages of VueJs?May 05, 2025 am 12:06 AM

The main disadvantages of Vue.js include: 1. The ecosystem is relatively new, and third-party libraries and tools are not as rich as other frameworks; 2. The learning curve becomes steep in complex functions; 3. Community support and resources are not as extensive as React and Angular; 4. Performance problems may be encountered in large applications; 5. Version upgrades and compatibility challenges are greater.

Netflix: Unveiling Its Frontend FrameworksNetflix: Unveiling Its Frontend FrameworksMay 04, 2025 am 12:16 AM

Netflix uses React as its front-end framework. 1.React's component development and virtual DOM mechanism improve performance and development efficiency. 2. Use Webpack and Babel to optimize code construction and deployment. 3. Use code segmentation, server-side rendering and caching strategies for performance optimization.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software