For the sake of data security, the queried table data is returned to the front end in the form of Base64 data, which needs to be analyzed and processed by the front end itself. It would be much easier to return to www.xxx.xx/aa.xlsx.

Example: Show the following body field data, which is the table data.

Base64 form of file download processing

Wechat applet processing

  • Base64 data is converted into an ArrayBuffer using wx.base64ToArrayBuffer(data)
  • GetFileSystemManager writes data locally (writeFile)
  • Use wx.openDocument to open the generated file.
const fileReg = /\.pdf$|\.xlsx$|\.doc$|\.docx$|\.xls$/i; filename = filename.trim(); if (! Filereg.test (filename)) {toast(" WARN needs to set file format, XLSX is turned on by default "); filename = `${filename}.xlsx`; } const _fsm = wx.getFileSystemManager(); _fsm && _fsm.writeFile({ filePath: `${wx.env.USER_DATA_PATH}/${filename}`, data: wx.base64ToArrayBuffer(base64Data), encoding: "utf8", success: function(e) { wx.openDocument({ filePath: ${wx.env.USER_DATA_PATH}/${filename} ', showMenu: true, success: function() {}, fail: function() {toast(" file open failed "); }}); }, fail: function(e) { console.log(e); If (e && e.e rrMsg) {toast ((e && e.e rrMsg) | | "file is written to fail"); } }, complete: function() {}, });Copy the code

H5 processing

  • Convert Base64 to Blob
  • Generate a link using url.createObjecturl
  • Create a link to open
function _downloadFileH5(content, fileName) { let base64ToBlob = function(code) { let _base64Data = code; let contentType = "text/plain"; if (code.indexOf("; base64") > -1) { let _parts = code.split("; base64,"); _base64Data = _parts[1]; contentType = _parts[0].split(":")[1]; } let raw = window.atob(content); let rawLength = raw.length; let uInt8Array = new Uint8Array(rawLength); for (let i = 0; i < rawLength; ++i) { uInt8Array[i] = raw.charCodeAt(i); } return new Blob([uInt8Array], { type: contentType, }); }; let aLink = document.createElement("a"); let blob = base64ToBlob(content); let evt = document.createEvent("HTMLEvents"); evt.initEvent("click", true, true); // Block the browser's default behavior alink. download = fileName; aLink.href = URL.createObjectURL(blob); aLink.click(); }Copy the code

Url form file download processing

DownloadFile directly

function openFile(fileUrl = "", filename = "table.xlsx") { // #ifdef H5 window.location.href = fileUrl; / / # endif / / # ifdef MP - WEIXIN / / processing PDF suffix const fileReg = / \. PDF $| \. XLSX $| \. Doc $| \. Docx $| \. XLS $/ I; filename = filename.trim(); if (! Filereg.test (filename)) {toast(" WARN needs to set file format, XLSX is turned on by default "); filename = `${filename}.xlsx`; } wx.downloadFile({ url: fileUrl || "https://b.leka.club/table.xlsx", filePath: `${wx.env.USER_DATA_PATH}/${filename}`, success: function(res) { wx.openDocument({ filePath: res.filePath, showMenu: True, success: function() {}, fail: function() {toast(" file open failed "); }}); }, fail: function() {toast(" file download failed "); }}); // #endif }Copy the code