The vue.js front end needs to download the excel table file from the back-end server.

The

backend provides a RESTful for a get request. Currently, the backend url is opened with the a tag, but the request for the url needs to add token information to the header. Click on the a tag before adding token verification to download it directly. Now click to prompt that token does not exist. The header information for configuring token is in the request header of axios, and all other api is fine.

<a :href="downLoadSrc" download class="downLoad-btn">
//url
self.downLoadSrc="http://172.21.81.160:8085/storemonitor/api/v1.0/inspect/template";

clipboard.png

Apr.10,2022

you can download the file directly with ajax, and then you can process it in two ways:
1, and read it with the FileReader class readAsDataURL.
2, window.URL.createObjectURL (blob).
put the processed result directly into the href of label a.


use the a tag to construct an a tag in the correct return of the request

let downLoadSrc="http://http://172.21.81.160:8085/storemonitor/api/v1.0/inspect/template";
this.$axios.get(downLoadSrc)
      .then(res => {
          var btn = document.createElement("a"); 
          btn.setAttribute('download', '');// download
          btn.setAttribute('href', downLoadSrc);// href
          btn.click();// 
})

download can actually be added or not, indicating the name of the file. Do not add is the default file name of the file
when you click the a tag, the file download process, transcoding and other operations are completed by the browser, the browser can automatically detect the file extension to generate the corresponding file format.

Menu