簡體   English   中英

在 iPhone 瀏覽器上下載 VCF 文件

[英]Downloading VCF file on iPhone browser

我正在嘗試在我的移動 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);
  }

上面的代碼在使用 windows OS 的 chrome 瀏覽器上調試時運行良好。 它在 android 手機上的 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;
}

從vCard Specification 4開始,不建議使用mime類型的text / x-vcard。 僅啞劇類型的text / vcard(無x-)有效。 檢查iOS瀏覽器是否由於數據URL開頭的mime類型而失敗。

對我來說,它是這樣工作的:(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);
}

和 HTML

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

接受的答案不適用於下載具有特定文件名的文件

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