在网上打开word文件内容

时间:2014-08-13 05:22:25

标签: c# asp.net

我的详细信息文件夹中有单词文件,我想在浏览器中显示该单词文件。它给了我不是有效的虚拟路径错误

string filename = "http://something.in/management/details/" + DS.Tables["Table"].Rows[0]["DETAILS"].ToString();

if (filename != "")
{
    string path = Server.MapPath(filename);
    System.IO.FileInfo file = new System.IO.FileInfo(path);
    if (file.Exists)
    {
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
        Response.AddHeader("Content-Length", file.Length.ToString());
        Response.ContentType = "application/ms-word";
        Response.WriteFile(file.FullName);
        Response.End();
    }
    else
    {
        Response.Write("This file does not exist.");
    }
}

1 个答案:

答案 0 :(得分:1)

Server.MapPath(filename)不接受URL

请参阅thisthis了解

e.g。

string filename = 
Server.MapPath("~/management/details/" + DS.Tables["Table"].Rows[0]["DETAILS"].ToString());

您也可以使用其他类型的联合Server.MapPath here