/**
 * 本地下载一个文本文件。
 * @link https://ourcodeworld.com/articles/read/189/how-to-create-a-file-and-generate-a-download-with-javascript-in-the-browser-without-a-server
 * @param filename {string}
 * @param text {string}
 */
// function download (filename, text) {
//   const element = document.createElement('a')
//   element.setAttribute('href', `data:text/plain;charset=utf-8,${encodeURIComponent(text)}`)
//   element.setAttribute('download', filename)

//   element.style.display = 'none'
//   document.body.appendChild(element)

//   element.click()

//   document.body.removeChild(element)
// }

// iframe下载文件，没又身份验证的get接口可以使用
function iframeDownloads(lastUrl){
  var downloadFileUrl = window.g.API_URL + lastUrl
  var elemIF = document.createElement("iframe");
  elemIF.src = downloadFileUrl;
  elemIF.style.display = "none";
  document.body.appendChild(elemIF);
}
// 设备二进制文件
function blobDownloads (data, fileName) {
  if (!data) {
    return
  }
  // type: 'application/vnd.ms-excel'
  const url = window.URL.createObjectURL(new Blob([data]))
  const link = document.createElement('a')
  link.style.display = 'none'
  link.href = url
  link.setAttribute('download', fileName)
  document.body.appendChild(link)
  link.click()
  window.URL.revokeObjectURL(url)
  document.body.removeChild(link)// 下载完毕删除a标签
}

export default {
  // download,
  blobDownloads,
  iframeDownloads
}
