【问题标题】:Generate vCard that can be downloaded on Android using ASP.NET使用 ASP.NET 生成可在 Android 上下载的 vCard
【发布时间】:2012-08-11 20:00:12
【问题描述】:

我一直在尝试使用 ASP.NET (C#) 生成可下载到 Android 设备上的 vCard。

生成卡片的过程非常简单,所以我不太担心。我无法开始工作的是下载本身。

我将 vCard 附加到页面响应的代码如下所示:

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();
}

我不会费心展示生成过程,因为它并不重要,尽管页面采用的唯一参数是通过RESFUL URL接收的用户名,这要归功于web.config文件中的一些URL重写。因此 URL example.com/vcard/apbarratt 会为用户 apbarratt 生成 vCard。

GET 请求为此代码生成的响应如下所示:

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

这在我测试过的每一个浏览器中都能完美运行(不是 iOS,这是另一个以另一种方式解决的问题),Android 股票浏览器除外。在这些浏览器中,下载失败,文件名“未知”和“失败”一词,或者在其他设备上,用户名“apbarratt.vcf”和“进行中”一词似乎永远不会改变。

在opera mobile/mini等其他浏览器中不是问题。

我已经尝试了我能想到的所有可能的事情,阅读了很多关于类似问题的博客,以至于我梦想着整件事……它们真的很无聊……

无论如何,希望一些新鲜的眼睛能够帮助我。也许有人以前做过,可以分享一些代码,期待一些帮助。

安迪

【问题讨论】:

标签: c# android asp.net vcf-vcard


【解决方案1】:

我遇到了完全相同的问题:除了现有的 Droid Safari 浏览器之外,其他所有浏览器似乎都可以正常工作。我的解决方案是将文件作为文本读取,然后将其转换为 ASCII 字节。一旦我更改了我的代码,Droids(2.3 和 3.2)似乎很高兴。

这是一个代码 sn-p(来自我的基于 MVC 的项目):

public ActionResult GetContact()
{
    Response.Clear();
    Response.AddHeader("Content-disposition", string.Format("attachment; filename=\"{0}\";", "MyContact.vcf"));

               //  VERY IMPORTANT!!!

               //      Read the file as text, and then convert it to ASCII bytes.  
               //      If ReadAllBytes is used, extra stray characters seem to appear and DROID fails.

               //      Put the content type in the second parameter!!!


    var vCardFile = System.IO.File.ReadAllText(Server.MapPath("~/Contacts/MyContact.vcf"));
    return File(System.Text.Encoding.ASCII.GetBytes(vCardFile), "text/x-vcard");
}

希望这会有所帮助...

干杯。

【讨论】:

    【解决方案2】:

    不知道你是否解决了,但我遇到了同样的问题,其中一个绊脚石是,N 字段似乎应该有 5 个值,所以你应该插入一个额外的分号到最后(在你的例子中是 4 个),或者这样:

    N;LANGUAGE=en-us:Barratt;Andy;;;
    

    另一件事是,最好将内容类型设置为 text/vcard,这是现在的标准。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      • 2011-02-17
      • 2015-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多