我正在尝试编写一个 MVC3 应用程序,该应用程序动态构建一个 vCard(.vcf) 文件,供人们在扫描 QR 码时在移动设备上下载。我在这里找到了一个示例来构建和返回 vCard。经过一些操作后,我能够让它在我的台式机上运行,但每次我尝试在移动设备上下载文件时,下载都会失败。我曾认为它发送的标头有问题,所以我做了一些挖掘并在此处找到了一些有关 android 的 HTTP 标头的有用信息。不幸的是,即使调整标题也没有改变任何东西。
第一部分是控制器,稍后将采用 URL 参数。保持简单的测试。
public class APIController : Controller
{
public vCardResult vCard()
{
vCard card = new vCard
{
FirstName = "First",
LastName = "Last",
StreetAddress = "70 Street Adr.",
City = "Atlanta",
State = "GA",
CountryName = "United States",
Mobile = "5558675309",
Organization = "MyCompany",
HomePage = "www.Google.com",
JobTitle = "Software Developer",
Zip = "30318",
Email = "FirstLast@MyCompany.com",
};
return new vCardResult(card);
}
这是 vCard 模型
public class vCard
{
//I cut out the properties to save space
public override string ToString()
{
var builder = new StringBuilder();
builder.AppendLine("BEGIN:VCARD");
builder.AppendLine("VERSION:2.1");
builder.AppendLine("FN:" + FirstName + " " + LastName);
builder.AppendLine("N:" + LastName + ";" + FirstName);
builder.AppendLine("TEL;CELL:" + Mobile);
builder.AppendLine("TEL:");
builder.AppendLine("EMAIL;INTERNET:" + Email);
builder.AppendLine("TEL;FAX:");
builder.AppendLine("TITLE:" + JobTitle);
builder.AppendLine("ORG:" + Organization);
builder.AppendLine("ADR:;;" + StreetAddress + ";" + City + ";" + ";" + Zip + ";");
builder.AppendLine("REV:20120730T15034z");
builder.AppendLine("END:VCARD");
return builder.ToString();
}
}
最后是动作结果
public class vCardResult : ActionResult
{
private vCard _card;
protected vCardResult() { }
public vCardResult(vCard card)
{
_card = card;
}
public override void ExecuteResult(ControllerContext context)
{
var response = context.HttpContext.Response;
response.ContentType = "text/vcard";
response.AddHeader("Content-Disposition", "attachment; fileName=\"" + _card.FirstName + "_" + _card.LastName + ".VCF\"");
var cardString = _card.ToString();
var inputEncoding = Encoding.Default;
var outputEncoding = Encoding.GetEncoding("windows-1257");
var cardBytes = inputEncoding.GetBytes(cardString);
var outputBytes = Encoding.Convert(inputEncoding,
outputEncoding, cardBytes);
response.OutputStream.Write(outputBytes, 0, outputBytes.Length);
}
}
与我之前列出的示例没有太大不同,但我希望在我走得更远之前至少能实现这个功能。
我检查了从服务器发送的响应以及类似工作系统(我无法访问源代码的系统)的响应,但我没有看到很多差异,所以我不确定他们的 vCard 为什么会打开手机的联系人列表添加为新联系人,我的下载失败。答复如下。
示例响应(工作):
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Disposition: attachment; filename=5240000053568089.vcf
Content-Type: text/x-vcard
Transfer-Encoding: chunked
Date: Tue, 31 Jul 2012 21:18:31 GMT
110
BEGIN:VCARD
VERSION:2.1
FN:First Last
N:Last;First
TEL;CELL:5558675309
TEL:
EMAIL;INTERNET:FirstLast@MyCompany.com
TEL;FAX:
TITLE:Software Developer
ORG:MyCompany
ADR:;;70 Street Adr.;Atlanta;GA;30318;
REV:20120523T150346Z
END:VCARD
0
我的应用程序的响应
HTTP/1.1 200 OK
Cache-Control: private
Transfer-Encoding: chunked
Content-Type: text/vcard
Server: Microsoft-IIS/7.0
X-AspNetMvc-Version: 3.0
Content-Disposition: attachment; fileName=First_Last.vcf
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 31 Jul 2012 20:14:02 GMT
105
BEGIN:VCARD
VERSION:2.1
FN:First Last
N:Last;First
TEL;CELL:5558675309
TEL:
EMAIL;INTERNET:FirstLast@MyCompany.com
TEL;FAX:
TITLE:Software Developer
ORG:MyCompany
ADR:;;70 Street Adr.;Atlanta;GA;30346;
REV:20120730T15034z
END:VCARD
0
提前感谢您阅读本文以及您能够提供的任何帮助/建议!