2013-02-26 67 views
1

我需要下載一個文件。如果文件是爲用戶提早創建的,我應該爲他顯示一些消息。我可以檢查文件是否爲nullASP.NET MVC檢查下載文件爲空

@Html.ActionLink("Print", "Certificate", new{type=Model.Type, product = i.Id}) 

我的行動:

[HttpPost] 
    public FileResult Certificate(string type, int product) 
    { 
     var crt = DBQueryExecutor.GetCertificate(type, product, long.Parse(User.Identity.Name), null); 
     if(crt == null) return null; 
     byte[] rep = Pdf.CreateCertificate(crt); 
     return File(rep, System.Net.Mime.MediaTypeNames.Application.Pdf); 
    } 

你有什麼想法?

+0

我可以發送post請求,生成的文件並保存到文件系統,然後調用href.location =「...」。但這不是很好的情況。 – user571874 2013-02-26 16:00:39

回答

0

您可以返回顯示文件尚未就緒的另一個視圖,並在此第二個視圖上再次提供鏈接。

[HttpPost] 
public ActionResult Certificate(string type, int product) 
{ 
    var crt = DBQueryExecutor.GetCertificate(type, product, long.Parse(User.Identity.Name), null); 
    if(crt == null) return View("NullFile"); 
    byte[] rep = Pdf.CreateCertificate(crt); 
    return File(rep, System.Net.Mime.MediaTypeNames.Application.Pdf); 
} 
+0

文件生成一次。所有下一個嘗試文件將爲空。 – user571874 2013-02-26 15:39:57

+0

然後在第二個視圖上沒有鏈接並相應地更改消息? – Joe 2013-02-26 15:43:58

+0

在這種情況下,我的頁面將被重新加載。我想不重新加載。 – user571874 2013-02-26 15:58:47