downloadFile.js 829 B

1234567891011121314151617181920212223
  1. export default function downloadFile(url,fileName,changeState){
  2. changeState & changeState(true);
  3. let _url = "";
  4. if (url.indexOf("https://view.officeapps.live.com/op/view.aspx?src=") != -1)_url = url.split("https://view.officeapps.live.com/op/view.aspx?src=")[1];
  5. else _url = url;
  6. const x = new XMLHttpRequest();
  7. x.open("GET", _url, true);
  8. x.responseType = "blob";
  9. x.onload = function (e) {
  10. let content = x.response;
  11. let link = document.createElement("a");
  12. link.download = fileName;
  13. link.style.display = "none";
  14. let blob = new Blob([content]);
  15. link.href = URL.createObjectURL(blob);
  16. document.body.appendChild(link);
  17. link.click();
  18. document.body.removeChild(link);
  19. changeState & changeState(false);
  20. }
  21. x.send()
  22. }