2011-06-06 39 views
0
public ActionResult GetAttachment1(string projectID) 
     { 

       return File("~/Uploads/Project", "application/pdf", projectID); 

     } 

這個代碼給出了一個錯誤的文件下載......如何啓用在asp.net MVC 3

回答

4

你需要指定到File方法的絕對路徑。使用Server.MapPath將相對轉換爲絕對路徑:

public ActionResult GetAttachment1(string projectID) 
{ 
    string projectPath = Server.MapPath("~/Uploads/Project"); 
    string file = Path.Combine(projectPath, projectID); 
    // at this stage file will look something like this 
    // "c:\inetpub\wwwroot\Uploads\Project\foo.pdf". Make sure that 
    // this is a valid PDF file and pass it to the File method 

    return File(file, "application/pdf", projectID); 
}