0
    System.IO.StreamReader sr = new System.IO.StreamReader(Server.MapPath(strheadlinesid1));

    string line;

    while (sr.Peek() != -1)
    {
        line = sr.ReadLine();
        Response.Write("<tr>"+"<td>"+ Server.HtmlEncode(line) + "</td>"+"</tr>");
    }

我正在使用上面的代码读取文件。但这只是正确读取 .txt 文件(未正确读取 .doc、docx 和 .rtf)。谁能告诉如何在网络浏览器中阅读 .pdf 文件,例如在新标签页中打开 adobe 阅读器。谢谢

4

3 回答 3

2

要下载 PDF 文件,请使用您的 pdf 文件调用此代码:根据用户对其浏览器的设置,它可能会根据需要在新选项卡中打开。

public static void DownloadFile(string fname, bool forceDownload)
{
    string path = fname;
    if (fname.StartsWith("~"))
        path = Server.MapPath(fname);
    string name = Path.GetFileName(path);
    string ext = Path.GetExtension(path);
    string type = "";
    // set known types based on file extension  
    if (ext != null)
    {
        switch (ext.ToLower())
        {
            case ".htm":
            case ".html":
                type = "text/HTML";
                break;

            case ".txt":
                type = "text/plain";
                break;

            case ".pdf":
                type = "Application/pdf";
                break;

            case ".doc":
            case ".rtf":
                type = "Application/msword";
                break;

            case ".exe":
                type = "application/octet-stream";
                break;

            case ".zip":
                type = "application/zip";
                break;
        }
    }
    if (forceDownload)
    {
        Response.AppendHeader("content-disposition",
            "attachment; filename=" + name);
    }
    if (!string.IsNullOrEmpty(type))
        Response.ContentType = type;
    Response.WriteFile(path);
    Response.End();
}
于 2011-05-24T09:02:39.797 回答
1

您只能通过浏览器的插件在浏览器中阅读 pdf 文件,可从此处下载:http: //kb2.adobe.com/cps/331/331025.html

为了在浏览器中正确查看文件,您应该为其设置 mime 类型:

context.Response.ContentType = "application/pdf";
context.Response.AppendHeader("Content-Disposition", "attachment; filename = " + fileName);

有关 Mime 类型的更多信息:http: //www.w3schools.com/media/media_mimeref.asp

于 2011-05-24T08:52:11.600 回答
0

您的方法读取文件的原始内容。Txt 文件正确显示是因为它们的内容是纯文本、doc/rtf/pdf 和其他格式将需要专门的控件才能正确显示它们。

于 2011-05-24T08:48:06.850 回答