In this article I will explain with an example, how to create and download vCard (.VCF) file in ASP.Net Core (.Net Core) Razor Pages.
Image Folder (Directory) Location
The Image file is located inside the Images Folder of wwwroot Folder (Directory).
Model
The Model class consist of following properties.
public class ContactModel
{
public string Name { get; set; }
public string Organization { get; set; }
public string Title { get; set; }
public string MobileNumber { get; set; }
public string HomeNumber { get; set; }
public string WorkNumber { get; set; }
public string EmailAddress { get; set; }
public string Website { get; set; }
public string ImageUrl { get; set; }
}
Namespaces
You will need to import the following namespace.
Index PageModel (Code-Behind)
Inside the PageModel, first the private property of IWebHostEnvironment interface is created.
Then, the IWebHostEnvironment interface is injected into the Constructor (HomeController) using Dependency Injection method and the injected object is assigned to the private property of IWebHostEnvironment interface.
After that, a public property of ContactModel class is created.
The PageModel consists of following Handler methods.
Handler method for handling GET operation
Inside this Handler method, the ContactModel class properties are set with the appropriate values.
Handler method for handling POST operation
Inside this Handler method, 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.
For the contact Photo, image file is read from the Folder (Directory) named Images using IWebHostEnvironment interface.
Then, image file is converted into BYTE Array and then converted to BASE64 string which is ultimately appended to the StringBuilder class object.
Once the vCard details such as Name, Organization, Phone Numbers, Email Addresses, Websites and Photo of the contact is filled, the file is downloaded using the File function.
Note: The ContentType of Response class is set as text/vcard which informs the Browser that the file type is a vCard.
public class IndexModel : PageModel
{
private IWebHostEnvironment Environment;
public IndexModel(IWebHostEnvironment environment)
{
this.Environment = environment;
}
public ContactModel ContactModel { get; set; }
public void OnGet()
{
ContactModel = new ContactModel();
ContactModel.Name = "Mudassar Khan";
ContactModel.Organization = "ASPSnippets Private Limited";
ContactModel.Title = "Director";
ContactModel.MobileNumber = "9800000000";
ContactModel.HomeNumber = "6300000000";
ContactModel.WorkNumber = "7800000000";
ContactModel.EmailAddress = "mudassar.khan@aspsnippets.com";
ContactModel.Website = "www.mudassarkhan.com";
ContactModel.ImageUrl = "\\Images\\Mudassar.png";
}
public FileResult OnPostCreateVCF(ContactModel ContactModel)
{
StringBuilder sb = new StringBuilder();
sb.Append("BEGIN:VCARD\r\nVERSION:2.1");
sb.Append(System.Environment.NewLine);
sb.Append("N:");
sb.Append(ContactModel.Name.Split(' ')[0]);
sb.Append(";");
sb.Append(ContactModel.Name.Split(' ')[1]);
sb.Append(";");
sb.Append(System.Environment.NewLine);
sb.Append("FN:");
sb.Append(ContactModel.Name);
sb.Append(System.Environment.NewLine);
sb.Append("TEL;CELL:");
sb.Append(ContactModel.MobileNumber);
sb.Append(System.Environment.NewLine);
sb.Append("TEL;HOME:");
sb.Append(ContactModel.HomeNumber);
sb.Append(System.Environment.NewLine);
sb.Append("TEL;WORK:");
sb.Append(ContactModel.WorkNumber);
sb.Append(System.Environment.NewLine);
sb.Append("EMAIL;WORK:");
sb.Append(ContactModel.EmailAddress);
sb.Append(System.Environment.NewLine);
sb.Append("ORG:");
sb.Append(ContactModel.Organization);
sb.Append(System.Environment.NewLine);
sb.Append("TITLE:");
sb.Append(ContactModel.Title);
sb.Append(System.Environment.NewLine);
sb.Append("URL:");
sb.Append(ContactModel.Website);
sb.Append(System.Environment.NewLine);
string folderPath = this.Environment.WebRootPath + ContactModel.ImageUrl;
byte[] bytes = System.IO.File.ReadAllBytes(folderPath);
string base64 = Convert.ToBase64String(bytes, 0, bytes.Length);
sb.Append("PHOTO;ENCODING=BASE64;JPEG:");
sb.Append(base64);
sb.Append(System.Environment.NewLine);
sb.Append("END:VCARD");
return File(Encoding.ASCII.GetBytes(sb.ToString()), "text/vcard", "Mudassar.vcf");
}
}
Razor Page (HTML)
HTML Markup
Inside the Razor Page, the ASP.Net TagHelpers is inherited.
The HTML of Razor Page consists of an HTML Form consisting of an HTML Table which consists of an Image element and some Hidden Fields and a Submit Button.
The Submit Button has been set with the POST Handler method using the asp-page-handler attribute.
Note: In the Razor PageModel, the Handler method name is OnPostCreateVCF but here it will be specified as CreateVCF when calling from the Razor HTML Page.
@page
@model Create_Download_vCard_Core_Razor.Pages.IndexModel
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form method="post">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td rowspan="10" valign="top">
<input type="hidden" asp-for="@Model.ContactModel.ImageUrl" />
<img src="@Model.ContactModel.ImageUrl" />
</td>
</tr>
<tr>
<td style="width: 100px">Name:</td>
<td>
<input type="hidden" asp-for="@Model.ContactModel.Name" />
@Model.ContactModel.Name
</td>
</tr>
<tr>
<td style="width: 100px">Company Name:</td>
<td>
<input type="hidden" asp-for="@Model.ContactModel.Organization" />
@Model.ContactModel.Organization
</td>
</tr>
<tr>
<td style="width: 100px">Title (Position):</td>
<td>
<input type="hidden" asp-for="@Model.ContactModel.Title" />
@Model.ContactModel.Title
</td>
</tr>
<tr>
<td>Mobile Number:</td>
<td>
<input type="hidden" asp-for="@Model.ContactModel.MobileNumber" />
@Model.ContactModel.MobileNumber
</td>
</tr>
<tr>
<td>Home Number:</td>
<td>
<input type="hidden" asp-for="@Model.ContactModel.HomeNumber" />
@Model.ContactModel.HomeNumber
</td>
</tr>
<tr>
<td>Work Number:</td>
<td>
<input type="hidden" asp-for="@Model.ContactModel.WorkNumber" />
@Model.ContactModel.WorkNumber
</td>
</tr>
<tr>
<td>Email Address:</td>
<td>
<input type="hidden" asp-for="@Model.ContactModel.EmailAddress" />
@Model.ContactModel.EmailAddress
</td>
</tr>
<tr>
<td>Website:</td>
<td>
<input type="hidden" asp-for="@Model.ContactModel.Website" />
@Model.ContactModel.Website
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit" asp-page-handler="CreateVCF" /></td>
</tr>
</table>
</form>
</body>
</html>
Screenshots
Form
Downloaded vCard (.VCF) file
Demo
Downloads