Home Web Front-end H5 Tutorial HTML5 code to implement drag-and-drop batch upload of files

HTML5 code to implement drag-and-drop batch upload of files

Mar 30, 2018 am 11:10 AM
h5 html5 document

This article mainly shares with you the HTML5 code for drag-and-drop batch uploading of files. This component is implemented based on Vue.js. The UI framework is elementUI. The complete demo address is at https://github.com/Msxiaoma/upload-folder. Drag and drop to upload folders (only supported by chrome).

1. Component Description

  1. Drag multiple folders simultaneously

  2. Delete the specified folder

  3. Display the upload progress bar of the current folder

  4. Partial upload of folders exceeding 5M

The effect is as follows:

2. Problems encountered

  1. Drag and drop to read the file path under each folder

  2. How to display the progress bar of the currently uploaded folder

  3. Carry cookies across domains when uploading files

  4. Folder Fragmentation

3. Solution process

1. Drag and drop to read the file path under each folder

When performing drag and drop operations , the DataTransfer object is used to save data dragged to the browser through drag-and-drop actions. It can save one or more data, one or more data types

// 拖拽文件夹
dropFolders (e) {
  e.stopPropagation()
  e.preventDefault()
  var items = e.dataTransfer.items
  for (var i = 0; i < items.length; i++) {
    var item = items[i].webkitGetAsEntry()
    if (item) {
      this.checkFolders(item)
    }
  }
}

// 判断是否为文件夹
checkFolders (item) {
  if (item.isDirectory) {
    this.traverseFileTree(item)
  } else {
    this.$alert(&#39;只支持上传文件夹&#39;, &#39;提示&#39;, {
      confirmButtonText: &#39;确定&#39;
    })
  }
}

traverseFileTree (item, path, parentDir) {
  path = path || &#39;&#39;
  if (item.isFile) {
    item.file((file) => {
        let obj = {
          file: file,
          path: path + file.name,
          attr: item.attr
        }
        this.filesList.push(obj)
    })
  } else if (item.isDirectory) {
    var dirReader = item.createReader()
    dirReader.readEntries((entries) => {
      for (let i = 0; i < entries.length; i++) {
        entries[i].attr = item.attr
        this.traverseFileTree(entries[i], path + item.name + &#39;/&#39;, temp)
      }
    }, function (e) {
      console.log(e)
    })
  }
}
Copy after login

2. Progress bar of uploading folder

Files without fragmentation: According to the files in the folder The total number, calculate the percentage of each file in the folder, when a file is uploaded successfully, modify the folder process;

Fragmented files: After calculating the percentage of each file in the file, calculate the percentage of each file The percentage of the file. After each file is uploaded successfully, modify the process of the folder.

3. Carrying cookies across domains

When the server sets the response header

Access -Control-Allow-Origin: You must specify a clear domain name that is consistent with the requested web page, and cannot be *. Access-Control-Allow-Credentials: true

Set request header:

withCredentials:true

Supplement:

The difference between substring and substr

substr(start [, length]) returns a substring of the specified length starting from the specified position.

start:Required. The starting position of the desired substring. The first character in the string has index 0.
length: optional. The number of characters that should be included in the returned substring.

substring returns the substring located at the specified position in the String object, and returns a string containing the substring from start to the end (excluding end)

start: Indicates the starting position of the substring, and the index starts from 0.
end: Indicates the end position of the substring, the index starts from 0.

The above is the detailed content of HTML5 code to implement drag-and-drop batch upload of files. 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
3 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)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles