Example of Vue uploading files using formData format type

Example of Vue uploading files using formData format type

In Vue, we generally have front-end and back-end separation projects, that is, we need to use tools such as axios to send requests to the background to implement data operations.
Among them, file uploading is a relatively difficult one. This article teaches you how to upload files in five minutes.

1. For example, when uploading pictures, the backend needs to transfer formData type data from the frontend

<el-button type="primary" @click="uploadFile2()">Click to upload</el-button>
 <input type="file" @change="fileValueChange2()" ref="uploadFile2" enctype="multipart/form-data" style="display:none;" accept="image/jpeg,image/png,image/gif">

We use native input to achieve this.

uploadFile2(){
	// This event is triggered when the button is clicked // The function is to open the file upload pop-up box this.$refs.uploadFile2.click()
   },
   fileValueChange2(){
   // After selecting the file, the input change event will be triggered, and this function will be entered var formData = new FormData()
     // this.$refs.uploadFile2 is the method for getting DOM elements in Vue // All uploaded files can be obtained through files. If there are multiple files, formData.append('file',this.$refs.uploadFile2.files[0])
     // Request type must be set formData.append('type', "head");
     // If you need to pass the id, refer to the following code formData.append('id', this.id);
     // After configuration is complete, you only need to pass the formData variable to the backend insertNavigationUpload(formData).then(res=>{
       console.log('Is it simple? My friend')
     })
   },

I almost forgot one step. I won't say much about the secondary encapsulation of axios requests. I will only show the interface here.

export const tMessageNotification = data =>{
  return request({
    url:'/tMessageNotification/upload',
    method: 'POST',
    data,
    headers: {'Content-Type': 'application/json'},
  })
}

This is the end of this article about Vue using formData format type to upload files. For more relevant Vue file upload content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to upload files (FormData) using axios in vue
  • Vue formData realizes image upload
  • Vue uses formData to send data to the background
  • Vuejs uses FormData to implement ajax upload of image files
  • vue+element_ui uploads files and passes additional parameters
  • Detailed explanation of uploading files to the background example in Vue
  • Vue Element UI + OSS to realize the function of uploading files

<<:  Detailed process of building mysql5.7.29 on centos7 of linux

>>:  Should I use distinct or group by to remove duplicates in MySQL?

Recommend

JavaScript article will show you how to play with web forms

1. Introduction Earlier we introduced the rapid d...

Detailed Analysis of the Selection of MySQL Common Index and Unique Index

Suppose a user management system where each perso...

Solve the problem of docker images disappearing

1. Mirror images disappear in 50 and 93 [root@h50...

How to use Nginx to solve front-end cross-domain problems

Preface When developing static pages, such as Vue...

Problems encountered when uploading images using axios in Vue

Table of contents What is FormData? A practical e...

Detailed steps to install mysql5.7.18 on Mac

1. Tools We need two tools now: MySQL server (mys...

Detailed tutorial on installing ElasticSearch 6.x in docker

First, pull the image (or just create a container...

CSS3 realizes the mask barrage function

Recently I saw a barrage effect on B station call...

Teach you how to build the vue3.0 project architecture step by step

Table of contents Preface: 1. Create a project wi...

Detailed explanation of the problems and solutions caused by floating elements

1. Problem Multiple floating elements cannot expa...

Nodejs plug-in and usage summary

The operating environment of this tutorial: Windo...

Vue-cli creates a project and analyzes the project structure

Table of contents 1. Enter a directory and create...

HTML+CSS+JavaScript to achieve list loop scrolling example code

Description: Set a timer to replace the content of...

MySQL log trigger implementation code

SQL statement DROP TRIGGER IF EXISTS sys_menu_edi...