【问题标题】:Response TransmitFile - Nothing Happening响应 TransmitFile - 什么也没发生
【发布时间】:2013-02-07 20:14:55
【问题描述】:

帮助 Response.TransmitFile

发帖前,我在这里搜索了几个类似的帖子

Trouble with Response.WriteFile / Response.BinaryWrite / Response.TransmitFile (ASP.NET)

解决方案包括Response.End();

嗯,我已经添加了最后一行,但仍然没有发生任何事情。

我什至在 ASPX 页面的正文中包含了一个 <asp:Label ID="lblErrMsg" runat="server" />,但也没有写入任何内容。

public void DownloadFile(string nameOnly) {
  if (!String.IsNullOrEmpty(nameOnly)) {
    lblErrMsg.Text = "Transfer request for " + nameOnly;
    string filename = Server.MapPath(nameOnly);
    try {
      if (!String.IsNullOrEmpty(filename)) {
        Response.Buffer = true;
        Response.Clear(); // clear the buffer
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        if (-1 < filename.IndexOf(".avi")) {
          Response.ContentType = "video/x-msvideo";
          lblErrMsg.Text = "Content Type Set to Video.";
        } else if (-1 < filename.IndexOf(".pdf")) {
          Response.ContentType = "Application/pdf";
          lblErrMsg.Text = "Content Type Set to PDF.";
        } else if (-1 < filename.IndexOf(".rar")) {
          Response.ContentType = "Application/x-rar-compressed";
          lblErrMsg.Text = "Content Type Set to RAR.";
        } else {
          Response.ContentType = "Application/octet-stream";
          lblErrMsg.Text = "Content Type Set to Octet Steam.";
        }
        FileInfo file = new FileInfo(filename);
        Response.AddHeader("Content-Disposition", "attachment; filename=" + nameOnly);
        Response.AddHeader("Content-Length", file.Length.ToString());
        lblErrMsg.Text = "Content Headers added.";
        Response.TransmitFile(file.FullName);
        lblErrMsg.Text = "Transfer Completing...";
        Response.End();
        lblErrMsg.Text = "Transfer Complete.";
      } else {
        throw new Exception(string.Format("Server was unable to locate file \"{0}\".", nameOnly));
      }
    } catch (Exception err) {
      lblErrMsg.Text = err.Message;
    }
  }
}
  • 有人知道我做错了什么吗?

就像我说的,lblErrMsg.Text 是空白的,但没有显示下载对话框,而且似乎什么也没有发生。

更新:

我正在与 Aristos 合作。我使用他的建议修改了我的方法,但我的示例下载文件仍然没有显示:

private string Download(string nameOnly) {
  string outputLine = null;
  if (!String.IsNullOrEmpty(nameOnly)) {
    string filename = Server.MapPath(nameOnly);
    if (!String.IsNullOrEmpty(filename)) {
      try {
        Response.Buffer = false;
        Response.Clear(); // clear the buffer
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        if (-1 < filename.IndexOf(".avi")) {
          Response.ContentType = "video/x-msvideo";
        } else if (-1 < filename.IndexOf(".pdf")) {
          Response.ContentType = "Application/pdf";
        } else if (-1 < filename.IndexOf(".rar")) {
          Response.ContentType = "Application/x-rar-compressed";
        } else {
          Response.ContentType = "Application/octet-stream";
        }
        FileInfo file = new FileInfo(filename);
        Response.AddHeader("Content-Disposition", "attachment; filename=" + nameOnly);
        Response.AddHeader("Content-Length", file.Length.ToString());
        Response.TransmitFile(file.FullName);
      } catch (Exception err) {
        outputLine = err.Message;
      } finally {
        Response.Flush();
      }
    } else {
      outputLine = string.Format("Server was unable to locate file \"{0}\".", nameOnly);
    }
  }
  if (String.IsNullOrEmpty(outputLine)) {
    Response.Redirect("~/Default.aspx");
  }
  return outputLine;
}

【问题讨论】:

    标签: c# asp.net httpresponse


    【解决方案1】:

    尝试了解您有一个返回浏览器数据的管道。

    在您的代码中,您的行为就像您将两种数据发送回,一种发送到页面,另一种发送到下载数据 - 但实际上您不能这样做。就像您尝试在两个不同的文件上写入 - 将一个流打开到一个文件。

    所以选择以太发送文件,以太更新页面。

    我在这里描述了我认为下载文件的最佳方式:
    Side actions not happening after Response.WriteFile()
    What is the best way to download file from server
    Error handling when downloading file from ASP.NET Web Handler (.ashx)

    【讨论】:

    • 我实际上在事后添加了lblErrMsg 标签,以便调试并查看我的下载停止的位置。它仍然没有下载。
    • @jp2code 您不能同时使用页面更新和文件下载。只保留一个。
    • @jp2code 通常最好使用处理程序,原因有很多,其中之一是您的文件可能会从页面的 gZip 压缩中再次传递并中断。也有可能在您的磁盘上找不到,您是否检查过?并且最好使用 .Flush() 而不是 .End()
    • @jp2code 并确保关闭缓冲区! Response.Buffer = false; 缓冲区的意思是先把文件放到内存中,然后尝试全部发送,这可能会导致大量数据失败,缓冲区为真是当我们一点一点渲染页面时,我们喜欢将它们一起展示给用户。
    • @jp2code buffer = false 最先出现,flush 最后出现。 TransmitFile 在文件结束后也使用刷新。刷新是尝试在管道上发送任何内容,而不关闭连接。
    猜你喜欢
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 1970-01-01
    • 2012-09-14
    • 1970-01-01
    • 2019-09-20
    相关资源
    最近更新 更多