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

Introduction to user management under Linux system

Table of contents 1. The significance of users an...

Analysis of the reasons why Vue3 uses Proxy to implement data monitoring

Vue data two-way binding principle, but this meth...

Detailed explanation of js's event loop event queue in the browser

Table of contents Preface Understanding a stack a...

Nexus uses API to operate

Nexus provides RestApi, but some APIs still need ...

The difference between redundant and duplicate indexes in MySQL

MySQL allows you to create multiple indexes on a ...

JavaScript to achieve the idea of ​​​​snake game

The implementation idea of ​​the javascript game ...

Front-end state management (Part 2)

Table of contents 1. Redux 1.1. Store (librarian)...

Native JS to achieve drag photo wall

This article shares with you a draggable photo wa...

A magical MySQL deadlock troubleshooting record

background Speaking of MySQL deadlock, I have wri...