简体   繁体   English

强制文件保存对话框

[英]Force file save dialog

I'm having trouble trying to force a browser to open the file save dialog. 我在尝试强制浏览器打开文件保存对话框时遇到麻烦。 I'm using Content-Disposition header and attachment, also I'm using download attribute of the <a> tag. 我正在使用Content-Disposition标头和附件,也在使用<a>标签的download属性。 The trouble is that the dialog doesn't appear when there is a default directory for download is chosen in a browser options. 麻烦的是,当在浏览器选项中选择了默认下载目录时,对话框不会出现。 Currently I'm working in Chrome. 目前,我正在使用Chrome。

Here is a snapshot of how I'm trying to make browser show the dialog: 这是我尝试使浏览器显示对话框的快照:

var csvUrl = document.createElement('a');
csvUrl.href = url;
csvUrl.download = filename;
csvUrl.click();

This is how I'm returning a file from the server (using Apache CXF): 这是我从服务器(使用Apache CXF)返回文件的方式:

return Response.ok(csvData.toString())
               .header("Content-Disposition", "attachment; filename=" + filename)
               .build();

Thanks in advance! 提前致谢!

Unfortunately this isn't possible, you cannot force Chrome to show the dialog. 不幸的是,这是不可能的,您不能强制Chrome显示对话框。 The only thing you can do is show a notice for Chrome users like "If no save dialog box appears, check your downloads folder, your file might already be there!" 您唯一可以做的就是向Chrome用户显示一条通知,例如“如果没有出现保存对话框,请检查您的下载文件夹,您的文件可能已经存在!”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM