我正在尝试在我的移动网络应用程序中下载一个 vcf 文件,而不访问服务器。
function downloadVcf(filename, data) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/x-vcard;charset=utf-8,' + encodeURIComponent(data));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
上面的代码在 chrome 浏览器和 windows 操作系统上调试时运行良好。此外,它在安卓手机上的 chrome 浏览器上也能完美运行。但它不适用于 iPhone 浏览器 (safari)。它不是下载 vcf 文件,而是在浏览器中打开 VCF 文件。但它确实可以让用户将其导入其他应用程序(例如联系人)。但我想要的是在用户的 iPhone 中下载 VCF 文件。
请帮忙。
最佳答案
将 document.location.href
设置为您的数据 URI 的值应该有效:
function downloadVcf(data) {
// build data url
var url = 'data:text/x-vcard;charset=utf-8,' + encodeURIComponent(data);
// ask the browser to download it
document.location.href = url;
}
关于javascript - 在 iPhone 浏览器上下载 VCF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54871408/