如何在asp.net mvc 3中启用文件下载

时间:2011-06-06 06:39:13

标签: asp.net-mvc-3

 public ActionResult GetAttachment1(string projectID)
        {

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

        }

此代码出错......

1 个答案:

答案 0 :(得分: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);
}