2

I'm trying to start downloading files from server, now just with some hardcoded values for files which exists but for some reason the download does not start and no error is thrown.

This is the code I have:

public void ProcessRequest(HttpContext context)
{
    string destPath = context.Server.MapPath("~/Attachments/cover.txt");
    // Check to see if file exist
    FileInfo fi = new FileInfo(destPath);

    if (fi.Exists)
    {
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.AppendHeader("Content-Length", fi.Length.ToString());
        HttpContext.Current.Response.ContentType = "application/octet-stream";
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + "cover.txt");
        HttpContext.Current.Response.BinaryWrite(ReadByteArryFromFile(destPath));
        HttpContext.Current.Response.End();
    }
}

public bool IsReusable
{
    get
    {
        return false;
    }
}

private byte[] ReadByteArryFromFile(string destPath)
{
    byte[] buff = null;
    FileStream fs = new FileStream(destPath, FileMode.Open, FileAccess.Read);
    BinaryReader br = new BinaryReader(fs);
    long numBytes = new FileInfo(destPath).Length;
    buff = br.ReadBytes((int)numBytes);
    return buff;
}

I'm stepping in the code and no problem is occurring but as well no file download popup is shown in the browser.

Do you see anything wrong?

halfer
  • 19,824
  • 17
  • 99
  • 186
Laziale
  • 7,965
  • 46
  • 146
  • 262
  • Have you checked your header and ensure that the `byte[]` is indeed populated? – Greg Apr 28 '15 at 21:17
  • @Greg response header http://i.gyazo.com/005c1b12e8f4cb1bb55845160c934529.png – Laziale Apr 28 '15 at 21:21
  • I tried with pdf as well, as you can see the content of the actual pdf is written in the response tab but the document is not shown for download http://i.gyazo.com/c4038962e8a385257c8eb8cf257fc9a7.png – Laziale Apr 28 '15 at 21:37

2 Answers2

1

I believe the issue your having is that your calling HttpContext.Current. Since your utilizing a Generic Handler File I believe you'll want to utilize the context parameter being passed to your method signature. An example would be:

public void ProcessRequest (HttpContext context)
{
     // Build Document and Zip:
     BuildAndZipDocument(); 

     // Context:
     context.Response.ContentType = "application/zip";
     context.Response.AddHeader("content-disposition", "filename="Commodity.zip");
     zip.Save(context.Response.OutputStream);

     // Close:
     context.Response.End();
}

I believe if you utilize context, rather than HttpContext.Current it will resolve your issue.

Greg
  • 11,302
  • 2
  • 48
  • 79
  • nope, the output is still the same when swithcing the context. Also you've introduced BuildAndZipDocument method, why is that? Thx – Laziale Apr 28 '15 at 21:53
  • That is from code that I have that physically works, that is just an example method to perform a task. – Greg Apr 28 '15 at 21:54
  • Whats in the BuildAndZipDocument method, can you share that pls? Thanks – Laziale Apr 28 '15 at 21:56
  • What benefit will that give you to solve your question? This is building an Excel document, granted I have another that will build an Excel and populate a pdf catalog as well. – Greg Apr 28 '15 at 21:59
  • Never mind, thanks for your effort, but I'm still without answer to my question. – Laziale Apr 28 '15 at 22:04
  • I would share it, I just don't know how it would answer the question. There is an issue with either the header or the `byte[]`. – Greg Apr 28 '15 at 22:05
  • What can be wrong with the header, for example I'm trying to send pdf document, I have this header context.Response.AppendHeader("Content-Disposition", "attachment; filename=Test_PDF.pdf"); – Laziale Apr 28 '15 at 22:18
  • What is `zip` in this example? – KWallace May 28 '22 at 22:19
  • An instance of the zipped file that was populated via the static method. Allows me to control the disposal after each usage. – Greg May 30 '22 at 07:11
0

NEVER use "application/zip" in your Content-Type header for a ZIP file. NEVER! Confirmed bug is in IE6 which will corrupt the download.

If you want the most universal behavior for a binary file across the most browsers past and present ALWAYS use "application/octet-stream" just like you see it used everywhere else!!

header('Content-Type: application/octet-stream');

This overcomes the IE6 bug, assuming you care. Nonetheless, you achieve nothing by switching from application/octet-stream to application/zip, so you might as well stop wasting your time on that one and keep it application/octet-stream.

John Doe
  • 303
  • 1
  • 8