Table of Contents
Process Brief
Get the MD5 unique identification code of the file
文件切片
获取文件名 name
分片文件大小 chunkSize
文件切片 chunkList 列表
切片总数 chunks
切片大小  size
合并
Home Web Front-end Vue.js A 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.js File Upload file 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
Copy after login

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)
      }
    });
  }
}
Copy after login

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)
Copy after login

文件切片

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

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

获取文件名 name

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

分片文件大小 chunkSize

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

文件切片 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)  // 把截取的每一片数据保存到数组
              }
            }
Copy after login

切片总数 chunks

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

let chunks = chunkList.length
Copy after login

切片大小 size

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

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

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

合并

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

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

(学习视频分享: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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use Laravel to implement file upload and download functions How to use Laravel to implement file upload and download functions Nov 02, 2023 pm 04:36 PM

How to use Laravel to implement file upload and download functions Laravel is a popular PHP Web framework that provides a wealth of functions and tools to make developing Web applications easier and more efficient. One of the commonly used functions is file upload and download. This article will introduce how to use Laravel to implement file upload and download functions, and provide specific code examples. File upload File upload refers to uploading local files to the server for storage. In Laravel we can use file upload

Implement file upload and download in Workerman documents Implement file upload and download in Workerman documents Nov 08, 2023 pm 06:02 PM

To implement file upload and download in Workerman documents, specific code examples are required. Introduction: Workerman is a high-performance PHP asynchronous network communication framework that is simple, efficient, and easy to use. In actual development, file uploading and downloading are common functional requirements. This article will introduce how to use the Workerman framework to implement file uploading and downloading, and give specific code examples. 1. File upload: File upload refers to the operation of transferring files on the local computer to the server. The following is used

How to use gRPC to implement file upload in Golang? How to use gRPC to implement file upload in Golang? Jun 03, 2024 pm 04:54 PM

How to implement file upload using gRPC? Create supporting service definitions, including request and response messages. On the client, the file to be uploaded is opened and split into chunks, then streamed to the server via a gRPC stream. On the server side, file chunks are received and stored into a file. The server sends a response after the file upload is completed to indicate whether the upload was successful.

How to solve Java file upload exception (FileUploadException) How to solve Java file upload exception (FileUploadException) Aug 18, 2023 pm 12:11 PM

How to solve Java file upload exception (FileUploadException). One problem that is often encountered in web development is FileUploadException (file upload exception). It may occur due to various reasons such as file size exceeding limit, file format mismatch, or incorrect server configuration. This article describes some ways to solve these problems and provides corresponding code examples. Limit the size of uploaded files In most scenarios, limit the file size

How to implement FTP file upload progress bar using PHP How to implement FTP file upload progress bar using PHP Jul 30, 2023 pm 06:51 PM

How to use PHP to implement FTP file upload progress bar 1. Background introduction In website development, file upload is a common function. For the upload of large files, in order to improve the user experience, we often need to display an upload progress bar to the user to let the user know the file upload process. This article will introduce how to use PHP to implement the FTP file upload progress bar function. 2. The basic idea of ​​implementing the progress bar of FTP file upload. The progress bar of FTP file upload is usually calculated by calculating the size of the uploaded file and the size of the uploaded file.

File Uploading and Processing in Laravel: Managing User Uploaded Files File Uploading and Processing in Laravel: Managing User Uploaded Files Aug 13, 2023 pm 06:45 PM

File Uploading and Processing in Laravel: Managing User Uploaded Files Introduction: File uploading is a very common functional requirement in modern web applications. In the Laravel framework, file uploading and processing becomes very simple and efficient. This article will introduce how to manage user-uploaded files in Laravel, including verification, storage, processing, and display of file uploads. 1. File upload File upload refers to uploading files from the client to the server. In Laravel, file uploads are very easy to handle. first,

PHP file upload guide: How to use the move_uploaded_file function to handle uploaded files PHP file upload guide: How to use the move_uploaded_file function to handle uploaded files Jul 30, 2023 pm 02:03 PM

PHP file upload guide: How to use the move_uploaded_file function to handle uploaded files In developing web applications, file upload is a common requirement. PHP provides a convenient function move_uploaded_file() for processing uploaded files. This article will introduce you how to use this function to implement the file upload function. 1. Preparation Before starting, make sure that your PHP environment has been configured with file upload parameters. You can do this by opening php.in

Simplify file upload processing with Golang functions Simplify file upload processing with Golang functions May 02, 2024 pm 06:45 PM

Answer: Yes, Golang provides functions that simplify file upload processing. Details: The MultipartFile type provides access to file metadata and content. The FormFile function gets a specific file from the form request. The ParseForm and ParseMultipartForm functions are used to parse form data and multipart form data. Using these functions simplifies the file processing process and allows developers to focus on business logic.

See all articles