Leandro
Leandro

Reputation: 31

C# Asp Net Problem with download in Chrome 12

I have a problem in my project. I have some code for downloading content as: .doc, .zip etc

It was working fine until chrome update for version 12, after that the browser shows an error message: "Download Interrupted".

I already tested in other browsers (Chrome 11, FF and IE8) and everything works fine.

An code sample is:

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AppendHeader("Content-Type", "application/msword");
HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment; filename=" + filename + ".doc");
HttpContext.Current.Response.AppendHeader("Content-Transfer-Encoding", "binary");
HttpContext.Current.Response.Write(strBody);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();

Someone know what can be happening or how I can fix that?

Ps. Sorry my english, I'm brazilian :)

Upvotes: 3

Views: 4754

Answers (4)

Loy Bih Khuan
Loy Bih Khuan

Reputation: 1

Try this:

Response.Buffer = true;
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", "attachment; filename=yourExcelFileName.xlsx;");
Response.BinaryWrite(yourByteArray);
Response.End();

Upvotes: 0

krayknot
krayknot

Reputation: 1

string applicationName = Session["ApplicationName_UtilityDetail"].ToString();

    HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(Session["DownloadLink_UtilityDetail"].ToString());
    HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
    int bufferSize = 1;
    Response.Clear();
    Response.ClearHeaders();
    Response.ClearContent();
    Response.AppendHeader("Content-Disposition:", "attachment; filename=" + applicationName + ".zip");
    Response.AppendHeader("Content-Length", objResponse.ContentLength.ToString());
    Response.ContentType = "application/download";
    byte[] byteBuffer = new byte[bufferSize + 1];
    MemoryStream memStrm = new MemoryStream(byteBuffer, true);
    Stream strm = objRequest.GetResponse().GetResponseStream();
    byte[] bytes = new byte[bufferSize + 1];
    while (strm.Read(byteBuffer, 0, byteBuffer.Length) > 0)
    {
        Response.BinaryWrite(memStrm.ToArray());
        Response.Flush();
    }
    Response.Close();
    Response.End();
    memStrm.Close();
    memStrm.Dispose();
    strm.Dispose();

Upvotes: 0

Idrees Khan
Idrees Khan

Reputation: 7752

I am getting my binary data from sql server database with this code and it is working in all browser;

        DataTable table = SiteFileAccess.GetDataByFileID(fileId);
        byte[] b = (byte[])table.Rows[0]["FileData"];
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + table.Rows[0]["FileName"].ToString());
        Response.AddHeader("Content-Length", b.Length.ToString());
        Response.ContentType = "application/octet-stream";
        Response.BinaryWrite(b);
        Response.Flush();
        Response.End();

See the above code and fix it according to your need. Let me know If you need further information

Upvotes: 0

Bala R
Bala R

Reputation: 108937

According to an answer in this forum adding Response.End() solved the issue.

In your case, it would be

HttpContext.Current.Response.Write(strBody);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
HttpContext.Current.Response.Close();

Upvotes: 2

Related Questions