响应传输文件-没有发生任何事情



关于响应的帮助.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是空白的,但没有显示下载对话框,并且似乎什么都没有发生。

更新:

我和阿里斯托斯一起工作。我使用他的建议修改了我的方法如下,但我的示例下载文件仍然没有显示:

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

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

在代码中,你的行为就像是发回两种数据,一种是发送到页面,另一种是下载数据,但实际上你做不到。就像你试图在两个不同的文件上写作——让一个流打开到一个文件。

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

我在这里描述了我认为下载文件的最佳方式:
在Response.WriteFile()之后未发生副作用
从服务器下载文件的最佳方式是什么
从ASP.NET Web处理程序(.ashx)下载文件时的错误处理

最新更新