简体   繁体   English

在 iPhone 浏览器上下载 VCF 文件

[英]Downloading VCF file on iPhone browser

I am trying to download a vcf file in my mobile web app, without hitting the server.我正在尝试在我的移动 web 应用程序中下载一个 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);
  }

The code above works very well while debugging on chrome browser with windows OS.上面的代码在使用 windows OS 的 chrome 浏览器上调试时运行良好。 Also it works perfectly fine on chrome browser on android phones.它在 android 手机上的 chrome 浏览器上也能正常工作。 But it does not work on iPhone browser (safari).但它不适用于 iPhone 浏览器 (safari)。 Instead of downloading the vcf file it opens the VCF file in browser.它不是下载 vcf 文件,而是在浏览器中打开 VCF 文件。 But it does have option for user to import it to other apps (contacts for instance).但它确实可以让用户将其导入其他应用程序(例如联系人)。 But what I want is to download the VCF file in user's iPhone.但我想要的是在用户的 iPhone 中下载 VCF 文件。

Please help.请帮忙。

Setting document.location.href to the value of your data URI should work: 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;
}

Since vCard Specification 4, the mime type text/x-vcard is deprecated. 从vCard Specification 4开始,不建议使用mime类型的text / x-vcard。 Only the mime type text/vcard (without x-) is valid. 仅哑剧类型的text / vcard(无x-)有效。 Check if iOS browsers do fail because of the mime type in the beginning of your data url. 检查iOS浏览器是否由于数据URL开头的mime类型而失败。

For me it worked like this: (Chrome, Firefox, Edge, Android and Iphone)对我来说,它是这样工作的:(Chrome、Firefox、Edge、Android 和 Iphone)

var name = $('.people-name').text().replace(/\s+/g, '-').toLowerCase();

var vcard = vcard_begin+name+cel+tel+address+email+image+vcard_end;
// Create vcard content on variables
/*
Exemple:

var email = 'item1.EMAIL;type=email;type=work,pref:'+ mails + '\nitem1.X-ABLabel:Email\n';

*/
var filename = name + '.vcf';

var myFile = new File([vcard], filename, {type: "text/vcard;charset=utf-8"});

$(".vcard-download").on("click", function () {
 saveAs(myFile);
}

And HTML和 HTML

<span class="vcard-download">Save my vcard</span>

accepted answer doesn't work for downloading file with specific file name接受的答案不适用于下载具有特定文件名的文件

function downloadVcf(filename){
     var vcfString="BEGIN:VCARD\nVERSION:3.0\nREV:2022-12-30T05:56:13Z\nN;CHARSET=utf-8:sifr software\nEMAIL;INTERNET:masoomsanadi@yahoo.co.uk\nTEL:+91 9960706060\nADR:kolhapur, maharashtra, india\nEND:VCARD";
     var blob = new Blob([vcfString], {type: 'text/x-vcard'});
     var downloadUrl = URL.createObjectURL(blob);
     var a = document.createElement("a");
     a.href = downloadUrl;
     a.download = filename+".vcf";
     document.body.appendChild(a);
     a.click();
}

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

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