2011-04-18 26 views
0

我在我的asp.net mvc網站上有一個用戶上傳文件的網頁。現在我的問題是,我需要顯示一個頁面上的鏈接,點擊哪個用戶將能夠查看該文件。如何在硬盤上顯示文件asp.net mvc

用戶可以上傳文件或類型doc,docx和pdf。

我該怎麼做。

請幫幫忙,

感謝的

+1

當用戶上傳一個aspx文件時是cartful :) – Aristos 2011-04-18 10:38:52

回答

1

,你可以做最簡單的事情就是用System.IO.Directory.GetFiles(..),像這樣:

var myModel = new myModel { 
    Files = Directory.GetFiles(@"c:\temp") 
} 

然而,Directory.GetFiles(..)會給你一個字符串數組,其中可能有點難以使用。如果您需要以更多面向對象的方式使用這些文件,請參閱DirectoryInfoFileInfo類。

例子:

var directory = new DirectoryInfo(@"c:\temp"); 
foreach (FileInfo fi in directory.GetFiles()) { 
    Console.WriteLine(@"FileName: {0}", fi.Name); 
} 

MSDN參考:http://msdn.microsoft.com/en-us/library/07wt70x2.aspx

0

感謝的對回覆。

但我一直在尋找像這樣的東西,它解決了我的問題。

[HttpGet] 
    public ActionResult Index(string id) 
    { 
     string extension = id.Substring(id.IndexOf(".") + 1); 
     string contentType = string.Empty; 

     if (extension == "doc" || extension == "docx") 
     { 
      contentType = "application/msword"; 
     } 
     else if (extension == "pdf") 
     { 
      contentType = "application/pdf"; 
     } 

     if (string.IsNullOrEmpty(contentType)) 
     { 
      throw new Exception("Invalid file"); 
     } 
     return File(Server.MapPath("~/Docs/" + id), contentType); 
    } 

其中上述代碼中的id參數是文件名。

感謝的

相關問題