Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating vCard with photo that works on windows and ios - C#

Tags:

c#

.net

vcf-vcard

I'm currently writing a program for a client where they can export information as a VCard using .NET MVC.

I've got everything pulling through correctly when I download the vCard on Windows however the code I have does not seem to attach the image when I download the vCard off an IOS device.

This is the first vCard I have ever written so not something I am too familiar with. I am using V2.1 as its the only version I could get the image to display on a windows machine. I'm open to any suggestions here and also any info as to why the image does not pull through on IOS.

        // Building the V-Card
        var vCard = new StringBuilder();

        vCard.Append("BEGIN:VCARD");
        vCard.AppendLine();
        vCard.Append("VERSION:2.1");
        vCard.AppendLine();

        // Name
        vCard.Append($"N:  {model.sLastname};{model.sFirstname};");
        vCard.AppendLine();

        vCard.Append($"FN:{model.sFirstname} {model.sLastname}");
        vCard.AppendLine();

        // Company
        vCard.Append("ORG:");
        vCard.Append(model.sCompanyName);
        vCard.AppendLine();

        // Job Title
        vCard.Append("TITLE:");
        vCard.Append(model.sJobTitle);
        vCard.AppendLine();

        // Image
        FileModel file = FileModel.getByGuid(model.sProfilePicGuid);
        vCard.Append($"PHOTO;ENCODING=BASE64;TYPE={file.sFileType}:");
        vCard.Append(Convert.ToBase64String(file.FileData));
        vCard.AppendLine(string.Empty);
        vCard.AppendLine();

        // Tel
        vCard.Append("TEL");
        vCard.Append(";");
        vCard.Append("WORK");
        vCard.Append(";");
        vCard.Append("VOICE:");
        vCard.Append(model.sCompanyPhone);
        vCard.AppendLine();

        // Cell
        vCard.Append("TEL");
        vCard.Append(";");
        vCard.Append("CELL");
        vCard.Append(";");
        vCard.Append("VOICE:");
        vCard.Append(model.sPhoneNumber);
        vCard.AppendLine();

        // Email
        vCard.Append("EMAIL");
        vCard.Append(";");
        vCard.Append("PREF");
        vCard.Append(";");
        vCard.Append("INTERNET:");
        vCard.Append(model.sEmail);
        vCard.AppendLine();

        // Web site
        vCard.Append("URL");
        vCard.Append(";");
        vCard.Append("WORK:");
        vCard.Append(model.sWebsiteUrl);
        vCard.AppendLine();

        // Address
        string address = model.sAddress.Replace("\n"," ");
        vCard.Append($"ADR; WORK; PREF:; ; {address}; ; ; ; ");
        vCard.AppendLine();

        // End
        vCard.Append("END:VCARD");
        string result = vCard.ToString();

The below is what has been generated:

BEGIN:VCARD
VERSION:2.1
N:  du Preez;Armand;
FN:Armand du Preez
ORG:TestCompany
TITLE:Manager
PHOTO;ENCODING=BASE64;TYPE=image/jpeg:my Base64 string

TEL;WORK;VOICE:0745589983
TEL;CELL;VOICE:0745589983
EMAIL;PREF;INTERNET:[email protected]
URL;WORK:https://someurl.co.za
ADR; WORK; PREF:; ; 1 Westroad, Eastville; ; ; ; 
END:VCARD
like image 705
ArmandDuPreez Avatar asked Apr 25 '17 12:04

ArmandDuPreez


1 Answers

Use vCard.AppendLine() instead of vCard.Append("\n") because IOS devices uses \r instead of \n as line separator.

vCard.AppendLine() produces \r\n. That way it'll work in Windows and IOS (linux)

like image 105
aperezfals Avatar answered Oct 14 '22 15:10

aperezfals