SoFunction
Updated on 2025-04-04

A brief discussion on several ways and methods of file download in vue

1. Use blob to download files by sending post request to the backend

/**
 * @param {blob} res - Export data
 * @param {string} fileName - File export name
 * @param {string} format - Export file format
 * @return {*}
 */
function downBlob(res, fileName = "Export Report", format = "xlsx") {
  // Call the interface to get the file  const blob = new Blob([res], {
    type: "application/-excel"
  });
  // Download file name  fileName = fileName + parseInt(() * 9999) + "." + format;
  const linkNode = ("a");

   = fileName; // The download attribute of the a tag specifies the name of the download file   = "none";
   = (blob); // Generate a Blob URL  (linkNode);
  (); // Simulate a mouse click on the button
  (); // Release URL object  (linkNode);
}

2. Send post request to the backend and use url to download the file

/**
 * @param {string} fileName - File export name
 * @param {string} url - file url address
 * @return {*}
 */
function download(fileName, url) {
  fileName = fileName || "Export Report";
  let arr = (".");
  fileName += parseInt(() * 9999) + "." + arr[ - 1];
  const linkNode = ("a");
   = fileName; // The download attribute of the a tag specifies the name of the download file   = "none";
  if (.VUE_APP_TEM_PATH) {
    // Generate a Blob URL     = .VUE_APP_TEM_PATH + url;
  } else {
    // Generate a Blob URL     = url;
  }
  (linkNode);
  (); // Simulate a mouse click on the button  (); // Release URL object  (linkNode);
}

Global citation in progress

// Download the file through urlimport { download } from "@/utils/auth";
.$download = download;

// Download the file through blobimport { downBlob } from "@/utils/auth";
.$downBlob = downBlob;

Actual usage

// Download the file through urlthis.$download("Sales Statistics", );

// res download file via blobthis.$downBlob(res, "Details of the comparison between mall order and ERP order");

This is the article about the brief discussion on several methods and methods of file download in vue. This is the end. For more related vue file download content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!