无法使用 asp.net C# 正确读取文件

发布于 2024-11-09 12:32:06 字数 433 浏览 0 评论 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 reader 中打开。谢谢

    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>");
    }

I'm using the above code to read a file .But this is only reading .txt files properly(Not reading .doc,docx and .rtf properly). And Can anybody tell how to read .pdf files in web browser like opening in a adobe reader in a new tab.Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灼痛 2024-11-16 12:32:06

要下载 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();
}

To download a PDF file call this code with your pdf file: Depending on the user's settings for their browser, it may open in a new tab as you want.

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();
}
蓝咒 2024-11-16 12:32:06

您只能通过浏览器的插件在浏览器中阅读 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

You can read the pdf file in the browser only by add-in for your browser, downloadable from here:
http://kb2.adobe.com/cps/331/331025.html

For correct view the files in the browser you should set the mime types for it:

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

More about the Mime types:
http://www.w3schools.com/media/media_mimeref.asp

懒猫 2024-11-16 12:32:06

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

Your approach reads the raw content of the file. Txt-Files display correctly because their content is plain text, doc/rtf/pdf and other formats will require specialized controls to display them properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文