I have been trying for quite some time now to generate a vCard using ASP.NET (C#) that can be downloaded onto an Android device.
The process of generating the card is quite simple and so I'm not too worried about it. It's the download itself that I can't get to work.
My code for attaching the vCard to the page response looks like this:
public void downloadCard()
{
//generate the vCard text
string vCard = generateCard();
//create the filename the user will download the file as
string filename = HttpUtility.UrlEncode(username + ".vcf", System.Text.Encoding.UTF8);
//get a reference to the response
HttpResponse response = HttpContext.Current.Response;
//clear the response and write our own one.
response.Clear();
response.ContentType = "text/x-vcard";
response.AddHeader("Content-Disposition", "attachment; filename=" + filename + ";");
response.Write(vCard);
response.End();
}
I won't bother showing the generation process as it's not really important though the only parameter the page takes is for a username which is received through a RESFUL URL thanks to some URL rewriting in the web.config file. So the URL example.com/vcard/apbarratt produces the vCard for the user, apbarratt.
The response that a GET request produces for this code looks like this:
200 OK
Date: Wed, 15 Aug 2012 13:49:56 GMT
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Disposition: attachment; filename=apbarratt.vcf;
Content-Length: 199
Server: Microsoft-IIS/7.5
Content-Type: text/x-vcard; charset=utf-8
Cache-Control: private
BEGIN:VCARD
VERSION:2.1
N;LANGUAGE=en-us:Andy Barratt
FN:Andy Barratt
TEL;CELL;VOICE:07000000000
URL;WORK:http://example.com
EMAIL;INTERNET:apbarratt@example.com
END:VCARD
This works perfectly in every single browser that I have tested it in (not iOS, that's another issue that has been solved in another way), except for Android stock browsers. In these browsers, the download fails, either with the filename "unknown" and the term "failed" or on other devices with the username "apbarratt.vcf" and the term "In progress" which doesn't ever seem to change.
The issue is not a problem in other browsers such as opera mobile/mini.
I've tried every possible thing I can think of, reading so many blogs on similar issues that I'm having dreams about the whole thing... they're really dull...
Anyway, hopefully some fresh eyes will be able to help me. Perhaps someone has done this before and could share some code, looking forward to some help.
Andy