2012-08-03 56 views
0

大家好我有一個FileUpload控件,用戶可以上傳圖片和Doc文件,並且我將路徑的Url保存在我的數據庫中 現在當我從數據庫中檢索數據時,我想檢查文件是.doc還是圖像如果它是文檔它將打開文件我的問題是我該如何做圖像我如何檢查它的圖像我必須在圖像控件中顯示圖像從未在MVC3中的圖像控件上工作如何在數據庫中顯示圖像並在MVC3中顯示在視圖中?

這裏是我的控制器代碼

 public ActionResult ViewAttachments(string AttachmentName) 
    { 
     try 
     { 
      AttachmentName = Session["AttachmentUrl"].ToString(); 
      var fs = System.IO.File.OpenRead(Server.MapPath(" "+ AttachmentName+" ")); 
      return File(fs, "application/doc", AttachmentName); 
     } 
     catch 
     { 
      throw new HttpException(404, "Couldn't find " + AttachmentName); 
     } 
    } 

我使用一個在這裏aspx頁面內容的HTML我不得不使用,我必須改變這裏的任何建議認罪什麼碼e

回答

0

你可以做的是對擴展進行一個簡單的檢查,或者返回你正在做的FILE結果或者一個包含簡單圖像控件的局部視圖。

要擴大你的代碼,你可以做這樣的事情:

public ActionResult ViewAttachments(string AttachmentName) 
    { 
     try 
     { 
      AttachmentName = Session["AttachmentUrl"].ToString(); 
      var fs = System.IO.File.OpenRead(Server.MapPath(" " + AttachmentName + " ")); 
      var ext = Path.GetExtension(fs.Name); 

      switch (ext) 
      { 
       case "doc": 
        return File(fs, "application/doc", AttachmentName); 
       case "jpg": 
       case "jpeg": 
       case "png": 
        return PartialView("_imgControl", "http://www.mysite.com/downloads/" + AttachmentName + ext); 
      }     
     } 
     catch 
     { 
      throw new HttpException(404, "Couldn't find " + AttachmentName); 
     } 
    } 

然後在你的部分觀點,你可以只返回模型對象(在這種情況下,圖像路徑的網址,您的網站)一個標準的html圖像控件。