In this article I will explain with an example, how to create and download vCard (.VCF) file in ASP.Net using C# and VB.Net.
Image Folder (Directory) Location
The Image file is located inside the Images Folder of ASP.Net project.
HTML Markup
The HTML markup consists of following controls:
Image – For displaying image.
The Image control has been assigned with an ImageUrl property set with the path of the image file.
Label – For displaying vCard details.
Button – For creating and downloading vCard.
The Button has been assigned with an OnClick event handler.
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td rowspan="10" valign="top">
<asp:Image ID="imgPhoto" runat="server" ImageUrl="~/Images/Mudassar.png" />
</td>
</tr>
<tr>
<td style="width:100px">Name:</td>
<td><asp:Label ID="lblName" runat="server" Text="Mudassar Khan" /></td>
</tr>
<tr>
<td style="width:100px">Company Name:</td>
<td><asp:Label ID="lblOrganization" runat="server" Text="ASPSnippets Private Limited" /></td>
</tr>
<tr>
<td style="width:100px">Title (Position):</td>
<td><asp:Label ID="lblTitle" runat="server" Text="Director" /></td>
</tr>
<tr>
<td>Mobile Number:</td>
<td><asp:Label ID="lblMobileNumber" runat="server" Text="9800000000" /></td>
</tr>
<tr>
<td>Home Number:</td>
<td><asp:Label ID="lblHomeNumber" runat="server" Text="6300000000" /></td>
</tr>
<tr>
<td>Work Number:</td>
<td><asp:Label ID="lblWorkNumber" runat="server" Text="7800000000" /></td>
</tr>
<tr>
<td>Email Address:</td>
<td><asp:Label ID="lblEmailAddress" runat="server" Text="mudassar.khan@aspsnippets.com" /></td>
</tr>
<tr>
<td>Website:</td>
<td><asp:Label ID="lblWebsite" runat="server" Text="www.mudassarkhan.com" /></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="txtCreate" runat="server" Text="Create VCF" OnClick="CreateVCF" /></td>
</tr>
</table>
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Text;
VB.Net
Imports System.IO
Imports System.Text
Creating and Downloading vCard (.VCF) file using C# and VB.Net
When the Create VCF Button is clicked, first a StringBuilder class object is created.
Then, the information for creating the vCard (.VCF) is read from their respective fields and appended to the StringBuilder class object using Append method.
After that, the image file is converted into BYTE Array using ReadAllBytes method of File class.
The BYTE Array is then converted to BASE64 string which is ultimately appended to the StringBuilder class object.
Next, the Response class properties are set.
1. Content-Disposition – It is a response header indicating, the download file is an attachment and allows setting the file name.
2. ContentType – It informs the Browser about the file type. In this case it is vCard file.
Finally, the StringBuilder class object is written to the Response which initiates the File download operation.
protected void CreateVCF(object sender, EventArgs e)
{
StringBuilder fw = new StringBuilder();
fw.Append("BEGIN:VCARD\r\nVERSION:2.1");
fw.Append(Environment.NewLine);
fw.Append("N:");
fw.Append(lblName.Text.Split(' ')[0]);
fw.Append(";");
fw.Append(lblName.Text.Split(' ')[1]);
fw.Append(";");
fw.Append(Environment.NewLine);
fw.Append("FN:");
fw.Append(lblName.Text);
fw.Append(Environment.NewLine);
fw.Append("TEL;CELL:");
fw.Append(lblMobileNumber.Text);
fw.Append(Environment.NewLine);
fw.Append("TEL;HOME:");
fw.Append(lblHomeNumber.Text);
fw.Append(Environment.NewLine);
fw.Append("TEL;WORK:");
fw.Append(lblWorkNumber.Text);
fw.Append(Environment.NewLine);
fw.Append("EMAIL;WORK:");
fw.Append(lblEmailAddress.Text);
fw.Append(Environment.NewLine);
fw.Append("ORG:");
fw.Append(lblOrganization.Text);
fw.Append(Environment.NewLine);
fw.Append("TITLE:");
fw.Append(lblTitle.Text);
fw.Append(Environment.NewLine);
fw.Append("URL:");
fw.Append(lblWebsite.Text);
fw.Append(Environment.NewLine);
byte[] bytes = File.ReadAllBytes(Server.MapPath("~/Images/Mudassar.png"));
string base64 = Convert.ToBase64String(bytes, 0, bytes.Length);
fw.Append("PHOTO;ENCODING=BASE64;JPEG:");
fw.Append(base64);
fw.Append(Environment.NewLine);
fw.Append("END:VCARD");
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=Mudassar.vcf");
Response.Charset = "";
Response.ContentType = "text/vcard";
Response.Output.Write(fw.ToString());
Response.Flush();
Response.End();
}
VB.Net
Protected Sub CreateVCF(ByVal sender As Object, ByVal e As EventArgs)
Dim fw As StringBuilder = New StringBuilder()
fw.Append("BEGIN:VCARD" & vbCrLf & "VERSION:2.1")
fw.Append(Environment.NewLine)
fw.Append("N:")
fw.Append(lblName.Text.Split(" "c)(0))
fw.Append(";")
fw.Append(lblName.Text.Split(" "c)(1))
fw.Append(";")
fw.Append(Environment.NewLine)
fw.Append("FN:")
fw.Append(lblName.Text)
fw.Append(Environment.NewLine)
fw.Append("TEL;CELL:")
fw.Append(lblMobileNumber.Text)
fw.Append(Environment.NewLine)
fw.Append("TEL;HOME:")
fw.Append(lblHomeNumber.Text)
fw.Append(Environment.NewLine)
fw.Append("TEL;WORK:")
fw.Append(lblWorkNumber.Text)
fw.Append(Environment.NewLine)
fw.Append("EMAIL;WORK:")
fw.Append(lblEmailAddress.Text)
fw.Append(Environment.NewLine)
fw.Append("ORG:")
fw.Append(lblOrganization.Text)
fw.Append(Environment.NewLine)
fw.Append("TITLE:")
fw.Append(lblTitle.Text)
fw.Append(Environment.NewLine)
fw.Append("URL:")
fw.Append(lblWebsite.Text)
fw.Append(Environment.NewLine)
Dim bytes As Byte() = File.ReadAllBytes(Server.MapPath("~/Images/Mudassar.png"))
Dim base64 As String = Convert.ToBase64String(bytes, 0, bytes.Length)
fw.Append("PHOTO;ENCODING=BASE64;JPEG:")
fw.Append(base64)
fw.Append(Environment.NewLine)
fw.Append("END:VCARD")
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=Mudassar.vcf")
Response.Charset = ""
Response.ContentType = "text/vcard"
Response.Output.Write(fw.ToString())
Response.Flush()
Response.End()
End Sub
Screenshots
Form
Downloaded vCard (.VCF) file
Demo
Downloads