2012-08-15 18 views
3

我一直在嘗試使用可以下載到Android設備上的ASP.NET(C#)生成電子名片。生成可以使用ASP.NET在Android上下載的電子名片

生成卡的過程非常簡單,所以我並不太擔心。這是下載本身,我無法工作。

我對電子名片附加到頁面響應代碼如下所示:

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生成電子名片。

一個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:[email protected] 
END:VCARD 

這完全適用於每一個瀏覽器,我在(未iOS版已經測試過它,這是一個已經在解決了另一個問題另一種方式),除了Android股票瀏覽器。在這些瀏覽器中,下載失敗,無論是文件名「未知」和術語「失敗」,或在其他設備上使用用戶名「apbarratt.vcf」和術語「進行中」,似乎並未改變。

這個問題在其他瀏覽器如opera mobile/mini中不是問題。

我已經試過一切可能的事情我能想到的,讀書對我有整個事情的夢想類似的問題,所以許多博客......他們真的沉悶。

無論如何,希望有一些新鮮的眼睛能夠幫助我。也許有人已經做到了,可以分享一些代碼,期待一些幫助。

安迪

+0

這可能是相關的:http://stackoverflow.com/questions/4381766/how-to-get-a-vcard-vcf-file-into-android-contacts-from-website。 – 2012-08-15 14:08:36

回答

0

不知道你已經解決了,但我遇到同樣的問題和絆腳石是一個,即N字段似乎預計將有5個值,所以你應該插入一個額外的分號來結束(在你的例子其中4),或這樣的:

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

另一件事情是,它是更好地設置內容類型爲text /電子名片,這就是現在的標準。

0

我有完全相同的問題:除了股票Droid Safari瀏覽器似乎工作。我的解決方案是將文件作爲文本讀取,然後將其轉換爲ASCII字節。一旦我改變了我的代碼,Droids(2.3和3.2)似乎很高興。

下面的代碼片段(從我的基於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"); 
} 

希望這有助於...

乾杯。

相關問題