How to download excel stream files and set download file name in vue

How to download excel stream files and set download file name in vue

Overview

Export excel requirements. When you click to download the template or download the feedback results, axios initiates a backend interface request, and garbled characters appear when the returned data gets the response, as shown in the figure:

The following are some treatment methods.

1. Download via URL

That is, the backend provides the address of the file and you can download it directly using the browser

Download via window.location.href = file path

window.location.href = `${location.origin}/operation/ruleImport/template`

Via window.open(url, '_blank')

window.open(`${location.origin}/operation/ruleImport/template`)

The difference between these two usage methods:

  • window.location: jump to the current page, that is, relocate the current page; only the web pages of this website can be opened on the website.
  • window.open: Open the link in a new window; you can open the address of another website on the website.

2. Download through the a tag download attribute combined with the blob constructor

The download attribute of the a tag is a new addition to the HTML5 standard. Its function is to trigger the browser's download operation instead of navigating to the download URL. This attribute can be set to use a new file name when downloading.

The front end creates a hyperlink and receives the file stream from the back end:

axios.get(`/operation/ruleImport/template`, {
        responseType: "blob" //The data type of the server response, which can be 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream', the default is 'json'
    })
    .then(res => 
        if(!res) return
        const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' }) // Construct a blob object to process data and set the file type if (window.navigator.msSaveOrOpenBlob) { // Compatible with IE10
            navigator.msSaveBlob(blob, this.filename)
        } else {
            const href = URL.createObjectURL(blob) //Create a new URL to represent the specified blob object const a = document.createElement('a') //Create the a tag a.style.display = 'none'
            a.href = href //Specify the download link a.download = this.filename //Specify the download file name a.click() //Trigger the download URL.revokeObjectURL(a.href) //Release the URL object }
        // You don't need to create a link here, you can also download it directly by calling window.open(href)})
    .catch(err => {
        console.log(err)
    })

Note: When requesting the backend interface, add {responseType: 'blob'} to the request header; when setting the file name for download, you can directly set the extension. If not set, the browser will automatically detect the correct file extension and add it to the file.

3. Through js-file-download plugin

Install:

npm install js-file-download --S

use

import fileDownload from 'js-file-download'

axios.get(`/operation/ruleImport/template`, {
        responseType: 'blob' // data type returned})
    .then(res => {
        fileDownload(res.data, this.fileName)
    })

The above is the details of how to download Excel stream files and set the download file name in Vue. For more information about downloading Excel stream files and setting the download file name in Vue, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Example code of vue using Moment plugin to format time
  • The reason why vue3 removes filters
  • A brief analysis of how Vue displays time dynamically and in real time
  • Introduction and use of moment in vue development
  • VUE implements big turntable lottery
  • How to use Vue custom instructions to report Google Analytics event statistics
  • Vue Element front-end application development preparation for the development environment
  • How to handle the loss of parameters when refreshing the page when passing parameters to vue router
  • How to report business data in a Vue single page

<<:  Graphic tutorial on installing the latest version of MySQL server on Windows 7 64 bit

>>:  Solution to MySQL Installer is running in Community mode

Recommend

How to use ECharts in WeChat Mini Programs using uniapp

Today, we use uniapp to integrate Echarts to disp...

Implementation of HTML to PDF screenshot saving function

Using Technology itext.jar: Convert byte file inp...

Windows Server 2008 R2 Multi-User Remote Desktop Connection Licensing

At work, we often need remote servers and often e...

Brief analysis of centos 7 mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar

Baidu Cloud Disk: Link: https://pan.baidu.com/s/1...

mysql5.6.8 source code installation process

Kernel: [root@opop ~]# cat /etc/centos-release Ce...

Summary of some common configurations and techniques of Nginx

Preface This article lists several common, practi...

How to choose the right MySQL datetime type to store your time

When building a database and writing a program, i...

js implements shopping cart addition and subtraction and price calculation

This article example shares the specific code of ...

A complete guide to the Docker command line (18 things you have to know)

Preface A Docker image consists of a Dockerfile a...

Detailed tutorial on how to delete Linux users using userdel command

What is serdel userdel is a low-level tool for de...

Analysis of the process of building a LAN server based on http.server

I don’t know if you have ever encountered such a ...

Docker runs operations with specified memory

as follows: -m, --memory Memory limit, the format...

How to Rename Multiple Files at Once in Linux

Preface In our daily work, we often need to renam...

Summary of Ubuntu backup methods (four types)

Method 1: To use respin, follow these steps: sudo...