无法在 Safari 浏览器中下载 ICS 文件
Posted
技术标签:
【中文标题】无法在 Safari 浏览器中下载 ICS 文件【英文标题】:Not able to Download ICS file in safari browser 【发布时间】:2016-12-22 09:14:42 【问题描述】:我正在尝试在所有浏览器中下载 ICS 文件。 我可以在 chrome 中下载 ICS 文件,但不能在 safari 中下载。这是我使用的javascript代码。
if(isChrome)
var blob=new Blob([data]);
var link=document.createElement('a');
link.href=window.URL.createObjectURL(blob);
link.download=fileName+".ics";
link.click()
if(!isChrome)
` alert("not chrome");
var downloadLink = document.createElement('a');
downloadLink.setAttribute('href', 'data:application/octet;charset=utf-8,' + escape(data));
var fileName = fileName+'.ics';
downloadLink.setAttribute('download', fileName);
var clk = document.createEvent("MouseEvent");
clk.initEvent("click", true, true);
downloadLink.dispatchEvent(clk);
document.body.appendChild(downloadLink);
downloadLink.dispatchEvent(clk);
//download(data, fileName+".ics", "text/calendar");
在 Safari 浏览器中,正在下载生成的文件,但未附加该文件名。我得到的文件名是“未知”,但文件的大小是 3KB,内容只有文件名和扩展名没有被添加到文件中。
请告诉我如何解决这个问题。
提前致谢
【问题讨论】:
这个问题和Java有什么关系? Is there any way to specify a suggested filename when using data: URI?的可能重复 【参考方案1】:如果问题是您使用的是 safari 10.1 或更低版本,则问题可能是它不喜欢下载属性。 http://caniuse.com/#feat=download
【讨论】:
【参考方案2】:您应该指定 blob 类型。 类型:'文本/日历'
const blob = new Blob([data], type: 'text/calendar' );
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = filename + ".ics" ;
link.click();
使用它可以成功下载文件,但您必须手动将其添加到日历中。 Safari 不将 ics 包含在安全文件列表中,并且不会自动打开。
【讨论】:
以上是关于无法在 Safari 浏览器中下载 ICS 文件的主要内容,如果未能解决你的问题,请参考以下文章
移动 safari (iOS) 中的 javascript 不会下载日历邀请
打开自定义创建的 .ics 文件时,Iphone 中 safari 的奇怪行为