1. Link to obtain common files

Images, other files download Excel, TXT, Word, etc. This is the front end directly from oss to obtain temporary links

Get links:

You can view the official website of OSS at help.aliyun.com/document_de…

  • Install ali-oss first,

npm install ali-oss –save

  • You can also import it directly in index.html (online or local)
<! -- Introducing online resources --><script src="https://gosspublic.alicdn.com/aliyun-oss-sdk-x.x.x.min.js"></script>  
Copy the code

The following fields are obtained from the background;

 let OSS = require('ali-oss');
let fs = require('fs');
let client = new OSS({
  // yourRegion Specifies the region where the Bucket is located. Take East China 1 (Hangzhou) as an example, set Region to OSS-CN-Hangzhou.
  region: 'yourRegion'.// Temporary access keys (AccessKey ID and AccessKey Secret) obtained from the STS service.
  accessKeyId: 'yourAccessKeyId'.accessKeySecret: 'yourAccessKeySecret'.// The SecurityToken obtained from the STS service.
  stsToken: 'yoursecurityToken'.// Enter the name of the Bucket.
  bucket: 'examplebucket'});let filename = 'Own filename'    
let url = client.signatureUrl(objectName, { 
response: { 
'content-disposition':`attachment; filename="The ${encodeURIComponent(filename)}`}});this.download(url,filename);  // Call the downloaded method uniformly

Copy the code

2. Audio and video access links

/ / url links to obtain the temporary files from the background, can preview can download / / filename for the custom file name / / the files into a blob link first, and then you can download await only in have async keyword function

async downloadClick(url,filename) { 
      let _this = this;
      const res=await fetch(url); 
      console.log(res)      
      const blob=await res.blob()
      const urlBlob = window.URL.createObjectURL(blob) // Create an object URL. Blob files downloaded without this step will fail
      console.log('urlBlob: ', urlBlob)
      let downloadUrl = urlBlob
      setTimeout(() = > {
        _this.download(urlBlob,filename); // Call the downloaded method uniformly
      }, 200)},Copy the code

3. Download

Use the a tag method to download

 download(url, filename) {  
  let _this = this;    
  const element = document.createElement('a')
  element.setAttribute('href', url)
  element.setAttribute('download', filename)
  element.style.display = 'none'
  element.click();
  console.log("Download successful");      
 },
Copy the code