0

Help with Response.TransmitFile

Before posting, I searched on here and found several similar threads like this one

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

Where the solution was to include Response.End();

Well, I have included that last little line, but still nothing is happening.

I've even included an <asp:Label ID="lblErrMsg" runat="server" /> in the body of the ASPX page, but nothing is writing to it either.

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;
    }
  }
}
  • Does anyone see what I've done wrong?

Like I said, lblErrMsg.Text is blank, but no download dialog is showing up and nothing appears to be happening.

UPDATE:

I'm working with Aristos. I have modified my method as follows using his suggestions, but my sample download file still does not show up:

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;
}
Community
  • 1
  • 1

1 Answers1

1

Try to understand that you have ONE pipeline that you return back to the browser data.

In you code you act like you send back two kind of data, one to the page and one to the download data - but you actually can't do that. Is like you try to write on two different files - having one stream open to one file.

So select ether send the file, ether update the page.

I have describe here what I think that is best way to download a file:
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)

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • I actually added the `lblErrMsg` label after the fact in an effort to debug and see where my download stopped. It still does not download. –  Feb 07 '13 at 20:29
  • @jp2code You can not use both Page Update and File Download. Keep only one. – Aristos Feb 07 '13 at 21:00
  • 1
    @jp2code In generally is better to use handler for many reasons, one of them is that your file is may pass again from the gZip compression of the page and breaks. Also there is the possibility to not found on your disk, do you have check that ?. And is also better to use `.Flush()` and not the .End() – Aristos Feb 07 '13 at 21:07
  • @jp2code and for sure close the buffer ! `Response.Buffer = false;` The buffer means that is place the file on memory first, then try to send it all in ones, this may fail to large amount of data, the buffer to true is when we render a page little by little and we like to show it all together to the user. – Aristos Feb 07 '13 at 21:10
  • Call `Response.Buffer = false` after `.Flush()`? –  Feb 07 '13 at 21:17
  • 1
    @jp2code The buffer = false go first of all, the flush go last of all. The `TransmitFile` also use the flush after the file ends. The flush is try to send anything on the pipeline, with out close the connection. – Aristos Feb 07 '13 at 21:18
  • Rats. I made those changes, but the download still does not work. I have updated my original post at the bottom (see the **UPDATE** section). Is this the correct way to do it? How do I detect errors? –  Feb 07 '13 at 21:31
  • @jp2code I see your code line by line and I do not find any issue, other than as I say you, avoid an aspx page for download a file, and try a handler. Of course double check if the file is found. – Aristos Feb 07 '13 at 21:38