SoFunction
Updated on 2025-04-12

How to download local pdf files for vue

vue download local pdf file

Use axios in vue to download

axios('/public/pdf/***.pdf', {//pdf location            responseType: 'blob', //Important code        }).then(res => {
            const url = (new Blob([]));
            const link = ('a');
             = url;
            let fileName = "Downloaded" //The file name saved to local            ('download', fileName);
            (link);
            ();
            (link);
            ();
        })

In fact, a tag a is created and download is called

<a href="/***.pdf" rel="external nofollow"  download="download">Click to download</a>

You can also download it in html, but you will encounter cross-domain problems. I used anywhere to test it. I can also use nginx to solve cross-domain problems, JSONP, ajax, postMessage, etc.

vue PDF download and preview (file stream format and base64 format)

1. PDF download (file stream format)

    downClick(row) {
      (row, 'pppppxxx');
      downLoadZDPdf({
        custId: '91140403MA7Y5MCH34',
        qrdType: '6'
      }).then(res => {
        const link = ('a'); // Create elements        const blob = new Blob([], {
          type: 'application/pdf;charset-UTF-8'
        });
         = 'none';
         = (blob); // Create a download link        // num++
        ('download',  + '.pdf'); // Name the downloaded file        (link);
        (); // Click to download        (link); // Download and remove elements        (); // Release the blob object      });
    },

2. Preview of the new window of PDF

 preview(row) {
      getZDDataOfYL({
        custId: '91140403MA7Y5MCH34',
        qrdType: '6'
      }).then(res => {
        (res, 'Preview=>>>>>>>>>>>>>');
        const blob = new Blob([], { type: 'application/pdf' });
         = (blob);
        ();// A new window opens, borrow a browser to print      });
    },

3. Base64 format

Need to convert base64 format

// base64 to file    dataURLtoBlob(baseData) {
      var bstr = atob(baseData);
      var n = ;
      var u8arr = new Uint8Array(n);
      while (n--) {
        u8arr[n] = (n);
      }
       = new Blob([u8arr], { type: `application/pdf;charset-UTF-	8;application/-excel` });
    },
    
getPDFDataOfYL({ fileId:  }).then(res => {
         = false;
        if () {
          // Preview          if (flag !== 'DOWN') {
             = res?.data?.data;
            const basedata = ;
             = 'data:application/pdf;base64,' + basedata;
             = ({ url: , CMapReaderFactory });
            (pdf => {
               = ;
            });
          } else { // download            (res?.data?.data);
            var reader = new FileReader();
            ();

             = function (e) {
              // Compatible with IE              if () {
                var bstr = atob((',')[1]);
                var n = ;
                var u8arr = new Uint8Array(n);
                while (n--) {
                  u8arr[n] = (n);
                }
                var blob = new Blob([u8arr]);
                (blob, );
              } else {
                // Conversion is completed, create a tag for download                const a = ('a');
                 = ; // Write your file name here                 = ;
                (a);
                ();
                (a);
              }
            };
          }
        }
      });

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.