实现文件下载

  1. getFile(fileUrl) {
  2. //在本页打开窗口
  3. let that = this;
  4. var x = new XMLHttpRequest();
  5. x.open("GET", fileUrl, true);
  6. x.responseType = "blob";
  7. x.onprogress = function(event) {
  8. //在这里监听文件下载的进度
  9. };
  10. x.onload = function(e) {
  11. var url = window.URL.createObjectURL(x.response);
  12. var a = document.createElement("a");
  13. a.href = url;
  14. a.download = fileUrl.substring(fileUrl.lastIndexOf('/')+1);
  15. a.click();
  16. };
  17. x.send();
  18. },