【问题标题】:How to show save as dialog box in Angular without changing browser settings如何在不更改浏览器设置的情况下在 Angular 中显示另存为对话框
【发布时间】:2019-09-09 05:53:58
【问题描述】:

使用的平台

前端:角度

后端:WebAPI

要求:需要显示另存为对话框,一旦文件从服务器(API - ActionResult)返回到 Angular(前端)

解释: 前端:通过FormData提交发送数据(动作链接) 后端:从操作结果返回文件

Save as Dilaog

(不改变浏览器设置,需要在代码中处理)

Browser settings permission

【问题讨论】:

  • @PatricioVargas 保存文件没问题。下载前必须显示“另存为对话框”。我已经尝试了该帖子中标记的解决方案。它在下载之前不显示“另存为对话框”(不覆盖浏览器设置)代码: var pom = document.createElement('a'); pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent('Hello world!')); pom.setAttribute('下载', 'test.txt'); if (document.createEvent) { var event = document.createEvent('MouseEvents'); event.initEvent('click', true, true); pom.dispatchEvent(事件); } 其他 { pom.click(); }

标签: angular browser download save-as


【解决方案1】:

1) 我们无法通过 javascript 更改浏览器设置,但您仍然可以提示带有下载属性的对话框,请查看以下链接

http://paxcel.net/blog/savedownload-file-using-html5-javascript-the-download-attribute-2/

这只能从前端触发,以自动提示您必须从控制器单击 javascript 按钮的对话框

【讨论】:

  • 当浏览器设置为默认状态时,即使下载属性代码: var pom = document.createElement('a'); 也不会显示提示pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent('Hello world!')); pom.setAttribute('download', 'test.txt'); if (document.createEvent) { var event = document.createEvent('MouseEvents'); event.initEvent('click', true, true); pom.dispatchEvent(事件); } 其他 { pom.click(); }。这里我在 标签中添加了下载属性,仍然是文件自动下载(没有显示提示框)
  • 看来这是不可能的
猜你喜欢
  • 1970-01-01
  • 2011-04-01
  • 2011-05-04
  • 1970-01-01
  • 2010-12-17
  • 1970-01-01
  • 2013-02-21
  • 1970-01-01
相关资源
最近更新 更多