下载本地文件

新手上路,请多包涵

网页文件

<button mat-button (click) = "FileDownload">Download</button>

角四分量法

FileDownload()
{
  this.filePath = "D:\SamplePDF.pdf";
  document.loation.href = this.filePath;

}

在这里,我想在单击按钮时下载本地文件。我无法下载该文件。请帮忙。

PS:我也无法访问 window.open() 因为我正在使用 angular 4 应用程序。

原文由 Billy 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 175
1 个回答

你能试试这个片段吗

 function downloadFunc(){
    	var anchor=document.createElement('a');
    	anchor.setAttribute('href','D:/SamplePDF.pdf');
    	anchor.setAttribute('download','');
    	document.body.appendChild(anchor);
    	anchor.click();
    	anchor.parentNode.removeChild(anchor);
  }

原文由 Patharraj 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题