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

1

6

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
Musaceous answered 25/4, 2017 at 12:22 Comment(4)
IOS devices are unix like devices. So I would look at the return "\n" and try just a "\r".Molten
Thank you for the quick response, I've tried the above, changing the return character breaks this completely on both windows and ios machines. I've edited my question to include the vCard string that is generated.Musaceous
It's currently working on Windows and IOS - The only thing is that the image is not working on IOS where it is on Windows.Musaceous
I think there is something wrong with the filename or path. If there is a "../" if front of filename remove.Molten
P
9

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)

Promotive answered 25/4, 2017 at 13:17 Comment(5)
Thank - I've changed everything to AppendLine(), all records are pulling through except the image on IOS.Musaceous
In the case of the image dont do vCard.AppendLine($"PHOTO;ENCODING=BASE64;TYPE={file.sFileType}:"); because this will break the line before the content of the image. In this case use vCard.Append($"PHOTO;ENCODING=BASE64;TYPE={file.sFileType}:");Promotive
Hi Alejandro, changing the AppendLine to Append seemed to have fixed the issue I was having displaying the Photos. Thank you very much, you're a champ.Musaceous
Please mark the answer as Correct, and vote for it. ThanksPromotive
@Musaceous Can you please share the final code , I also have the issue for it .Eugeniaeugenics

© 2022 - 2024 — McMap. All rights reserved.