2011-06-06 14 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); 
}