Asp.net: - 是物理路径,但应为虚拟路径

Asp.net: - is a physical path, but a virtual path was expected

我想从本地 PC 下载 excel 文件格式,所以我编写了如下代码

protected void btnDownloadExcelTemp_Click(object sender, EventArgs e)
{
    try
    {
        string strFileFormat = System.Configuration.ConfigurationManager.AppSettings["FormateFilePath"].ToString();
        string strFilePath = HttpContext.Current.Server.MapPath(strFileFormat + "/CMP_TEMPLATES.xlsx");
        HttpResponse response = HttpContext.Current.Response;
        response.Clear();
        response.AppendHeader("content-disposition", "attachment; filename=" + "CMP_TEMPLATES.xlsx");
        response.ContentType = "application/octet-stream";
        response.WriteFile(strFilePath);
        response.Flush();
        response.End();
    }
    catch (Exception)
    {            
        throw;
    }
}

strFileFormat<add key="FormateFilePath" value="D:/Name/CMP/CMP Excel Template"/>

因此在下载时出现错误

'D:/Name/CMP/CMP Excel Template/CMP_TEMPLATES.xlsx' is a physical path, but a virtual path was expected.

我不知道它期待什么路径。请推荐

首先阅读文档:https://msdn.microsoft.com/en-us/library/ms524632(v=vs.90).aspx

MapPath根据相对路径或虚拟路径生成物理路径,所以给它一个物理路径是没有意义的。您已经有了物理路径,因此您应该能够完全跳过该步骤。

protected void btnDownloadExcelTemp_Click(object sender, EventArgs e)
{
    try
    {
        string strFileFormat = System.Configuration.ConfigurationManager.AppSettings["FormateFilePath"].ToString();
        string strFilePath = strFileFormat + "/CMP_TEMPLATES.xlsx";
        HttpResponse response = HttpContext.Current.Response;
        response.Clear();
        response.AppendHeader("content-disposition", "attachment; filename=" + "CMP_TEMPLATES.xlsx");
        response.ContentType = "application/octet-stream";
        response.WriteFile(strFilePath);
        response.Flush();
        response.End();
    }
    catch (Exception)
    {            
        throw;
    }
}