【问题标题】:Opening a file of any extension in an iframe在 iframe 中打开任何扩展名的文件
【发布时间】:2011-10-30 12:28:41
【问题描述】:

我有一个 Web 应用程序,我需要在其中打开任何标准格式的文件,例如 iframe 中的 .doc/docx/.csv/.txt/.xls 。我怎样才能做到这一点?我尝试使用下面的示例代码,但它没有打开 iframe 中的所有文件格式。我收到一些 XML 错误。

var ext = GetExtension(fileName);
        switch (ext)
        {
            case "pdf":
                Response.ContentType = "Application/pdf";
                break;
            case "htm":
            case "html":
                Response.ContentType = "text/html";
                break;
            case "txt":
                Response.ContentType = "text/plain";
                break;
            case "doc":
                Response.ContentType = "Application/vnd.ms-word";
                break;
            case "xls":
            case "csv":
                Response.ContentType = "Application/vnd.ms-excel";
                break;
            case "ppt":
            case "pps":
                Response.ContentType = "Application/vnd.ms-powerpoint";
                break;
            default:
                Response.ContentType = "Application/unknown";
                break;
        }
        if (Response.ContentType != "Application/unknown")
        {
            Response.Flush();
            Response.WriteFile(fileName);
            Response.End();
        }

【问题讨论】:

  • 一件事要尝试,而不是 Response.Flush(); 使用 Response.Clear();

标签: c# asp.net


【解决方案1】:

尝试添加此标题

Response.AppendHeader("Content-Disposition", "attachment; filename=file.[EXTENSION]")

您应该在此处替换确切的 [EXTENSION]。喜欢pdf

Response.AppendHeader("Content-Disposition", "attachment; filename=file.pdf")

然后你也可以写

Response.BinaryWrite(File.ReadAllBytes(filename));
Response.Flush();
Response.End();

【讨论】:

    【解决方案2】:

    试试

    Response.BinaryWrite(File.ReadAllBytes(filename));
    

    filename 是文件的完整路径

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-12
      • 1970-01-01
      相关资源
      最近更新 更多