You must Sign In to post a response.
  • Category: .NET

    When Download process I meet this issue

    Hi

    when i download binary data from sql server more then one records i meet this issue

    Server cannot set content type after HTTP headers have been sent

    i use this code also

    page1.Response.Clear(); // Already have this
    page1.Response.ClearContent(); // Add this line
    page1.Response.ClearHeaders();

    but

    i meet this issue

    Server cannot set content type after HTTP headers have been sent
  • #765188
    Hi,
    Client can make only 1 request at a time and logically for 1 request there should be 1 response. The header of the request can be sent only once and before every other body data of the response and once it is sent the client browser starts taking action acording to the header received for that request.
    So set Response.Clear(); and Response.Buffer = true;/Response.BufferOutput = true; at the beginnng of Response so that the entire page will be processed before any content is sent to the requesting client.
    Use Response.flush after you get the response.

    Hope it helps.
    Regards,
    Shashikant Gurav
    shashikantgurav22@gmail.com

  • #765189
    Hi
    shashi

    My Code this
    private void download(DataTable dt)
    {
    Byte[] bytes = (Byte[])dt.Rows[0]["IMAGEDATA"];
    Response.Buffer = true;
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "image/jpeg";
    Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows[0]["IMGNAME"].ToString());
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.Close();
    }

    I called this method using loop when the code second looping

    meet this line
    Response.Charset = "";

    i meet this issue.

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #765200
    lookout below code, see if it worth

    Response.ContentType = "application/pdf";
    Response.AppendHeader("Content-Disposition", "attachment; filename=MyFile.pdf");
    // Write the file to the Response
    const int bufferLength = 10000;
    byte[] buffer = new Byte[bufferLength];
    int length = 0;
    Stream download = null;
    try
    {
    download = new FileStream(Server.MapPath("~/Files/Lincoln.txt"),
    FileMode.Open,
    FileAccess.Read);
    do
    {
    if (Response.IsClientConnected)
    {
    length = download.Read(buffer, 0, bufferLength);
    Response.OutputStream.Write(buffer, 0, length);
    buffer = new Byte[bufferLength];
    }
    else
    {
    length = -1;
    }
    }
    while (length > 0);
    Response.Flush();
    Response.End();
    }
    finally
    {
    if (download != null)
    download.Close();
    }

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #765201
    Hi,
    No need to write Flush(); and Close(); inside loop, try this:
    public void Download(int i)
    {
    Byte[] bytes = (Byte[])System.Text.Encoding.UTF8.GetBytes("shashi" + i);
    Response.Buffer = true;
    Response.ContentType = "image/jpeg";
    Response.AddHeader("content-disposition", "attachment;filename= demo" + i + ".txt");
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.BinaryWrite(bytes);
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
    for (int i = 0; i < 3; i++)
    {
    Download(i);
    }
    Response.End();
    }
    Hope it helps.
    Regards,
    Shashikant Gurav
    shashikantgurav22@gmail.com


Sign In to post your comments