[Vue] How to implement file download and export functions

In the Vue project, there are many ways to implement the file download and list export functions. The following is an example of two common methods.

Method 1: Use the server interface to implement downloading and exporting

This method usually requires providing a corresponding interface on the server side, and the front end calls the interface by sending a request to implement the download and export functions. Specific steps are as follows:

Implement download and export functions on the server side and provide corresponding interfaces, such as /api/download and /api/export.
In the Vue component, send a GET or POST request through axios or other HTTP request library, communicate with the backend's /api/download or /api/export interface and obtain the file stream.
Convert the file stream to a Blob object and create a URL address using the URL.createObjectURL() method.
Use window.open() or tags to trigger download or export operations.
Here is a code example:

// Download Document

axios.get('/api/download').then(response => {
    
    
  const blob = new Blob([response.data])
  const url = URL.createObjectURL(blob)
  window.open(url)
})

// List export

axios.post('/api/export', data).then(response => {
    
    
  const blob = new Blob([response.data])
  const url = URL.createObjectURL(blob)
  const link = document.createElement('a')
  link.href = url
  link.download = fileName
  document.body.appendChild(link)
  link.click()
})

Method 2: The front-end directly generates the file and downloads or exports it

This method is usually implemented by directly generating files through JavaScript code on the front end, and then using the download or export function of the browser. Specific steps are as follows:

Define a method in the Vue component to generate the content of the file.
Convert the file contents to a Blob object and create a URL address using the URL.createObjectURL() method.
Use window.open() or tags to trigger download or export operations.
Here is a code example:

// Download Document

const content = 'Hello, world'
const blob = new Blob([content], {
    
     type: 'text/plain' })
const url = URL.createObjectURL(blob)
window.open(url)

// List export

const data = [{
    
     id: 1, name: 'Alice' }, {
    
     id: 2, name: 'Bob' }]
const fileName = 'users.csv'
const csvContent = 'id,name\n' + data.map(row => `${
      
      row.id},${
      
      row.name}`).join('\n')
const blob = new Blob([csvContent], {
    
     type: 'text/csv' })
const url = URL.createObjectURL(blob)
const link = document.createElement('a')
link.href = url
link.download = fileName
document.body.appendChild(link)
link.click()

It should be noted that when using these methods, we need to pay attention to data security and compatibility issues. When sending requests and operating files, the accuracy and security of the data must be ensured, and compatibility issues between different browsers must be considered as much as possible.

Guess you like

Origin blog.csdn.net/Ctrl_cope/article/details/130614091