How to implement file download for form fields in Vue form processing

Vue.js is a popular JavaScript framework for building user interfaces. In Vue applications, we often need to handle form operations, and one of the common requirements is to implement file downloads. The following describes how to realize the file download of form fields in Vue form processing, and everyone can communicate together.

1. Use the HTML a tag to download files
The easiest way is to use the HTML a tag, set its href attribute as the link address of the file, and click the a tag to download the file.

HTML code:

<template>
  <div>
    <a :href="fileUrl" download>下载文件</a>
  </div>
</template>

 Vue code:

<script>
export default {
  data() {
    return {
      fileUrl: 'http://baidu.com/file_test.pdf'  // 文件的链接地址
    };
  }
};
</script>

 

Through the above code example, click the "Download File" link to download the file named file_test.pdf.

2. Use the Fetch API to download files
If you need to obtain the download link of the file through the back-end interface, you can use the Fetch API to download the file.

Vue code:

<script>
export default {
  methods: {
    downloadFile() {
      fetch('http://test.com/api/download', {
        method: 'GET',
        responseType: 'blob'  // 声明返回数据类型为二进制数据
      })
        .then(response => response.blob())
        .then(blob => {
          const url = window.URL.createObjectURL(new Blob([blob]));
          const link = document.createElement('a');
          link.href = url;
          link.setAttribute('download', 'file.pdf');
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        });
    }
  }
};
</script>

 HTML code:

<template>
  <div>
    <button @click="downloadFile">下载文件</button>
  </div>
</template>

 

Through the above code example, after clicking the "Download File" button, the Vue component will request the file from the backend interface, create a Blob object after obtaining the binary data, and dynamically create a tag, and assign the URL of the Blob object to the href attribute of the a tag , and set the download attribute to the name of the file to be downloaded, and then simulate clicking the a tag to download.

3. Use axios to download files
If axios has been used as the HTTP request library in the Vue application, file download can be realized through the characteristics of axios.

Vue code:

<script>
import axios from 'axios';

export default {
  methods: {
    downloadFile() {
      axios({
        url: 'http://test.com/api/download',
        method: 'GET',
        responseType: 'blob'  // 声明返回数据类型为二进制数据
      }).then(response => {
        const url = window.URL.createObjectURL(new Blob([response.data]));
        const link = document.createElement('a');
        link.href = url;
        link.setAttribute('download', 'file.pdf');
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      });
    }
  }
};
</script>

 HTML code:

<template>
  <div>
    <button @click="downloadFile">下载文件</button>
  </div>
</template>

 

Through the above code example, call the method in the Vue component axiosto send a GET request, and set the returned data to a binary data type. Then, dynamically create the a tag, and create the returned binary data as a Blob object and assign it to the href attribute of the a tag. Finally, simulate clicking on the a label to download.


      The above is how to realize the file download of form fields in Vue form processing, and provides three implementation methods: using HTML a tag, using Fetch API and using axios. According to the specific request method and the data format returned by the back-end interface, choose the most suitable method to realize file download. Hope this article is helpful to your needs of handling file downloads in Vue applications.

Guess you like

Origin blog.csdn.net/lwf3115841/article/details/132247337