You must Sign In to post a response.
  • Category: [About DotNetSpider]

    Response.flush() not working file not downloading

    Hai,
    Recently i m getting problem that bat file not downloading which was worked previously with as shown code below
    ========================================================
    string filePath = "";


    filePath = "E://ContractYarnIssue.bat";
    FileInfo file = new System.IO.FileInfo(filePath);
    if (file.Exists)
    {
    try
    {

    }
    catch (Exception)
    {
    //handle the situation gracefully.
    }
    //return the file


    string strFileExtName = strYarnIssueID + ".bat";
    Context.Response.Clear();
    Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileExtName);
    Context.Response.AddHeader("Content-Length", file.Length.ToString());
    Context.Response.ContentType = "application/octet-stream";
    Context.Response.WriteFile(file.FullName);
    Context.ApplicationInstance.CompleteRequest();
    Context.Response.Flush();

    Context.Response.Clear();


    }
    ========================================================
    Previously it work but now the file doest downloads,Can any one help ..


    Regards
    Vijay
  • #767200
    Hi,
    Try this:

    string strFileExtName = "1" + ".bat";
    Context.Response.Clear();
    Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileExtName + "; size=" + file.Length.ToString());
    //Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileExtName);
    Context.Response.AddHeader("Content-Length", file.Length.ToString());
    Context.Response.ContentType = "application/octet-stream";
    Context.Response.WriteFile(file.FullName);
    Context.ApplicationInstance.CompleteRequest();
    Context.Response.Flush();
    //Context.Response.Clear();
    Context.Response.End();

  • #767201
    Response.Flush method sends buffered output immediately. This method causes a run-time error if Response.Buffer has not been set to TRUE
    Be aware that if your IIS server is compressing the output with GZIP, then it will seem to ignore all Response.Flush calls. This is turned on by default in IIS7 and on Windows 7.
    And, if you are testing with Fiddler, be sure to turn on "Streaming" mode, or Fiddler will collect the flushed HTML and hold it until the connection is completed.
    use 'ClearHeaders' method as given below

    // Makes sure the page does not get cached
    context.Response.Cache.SetCacheability(HttpCacheability.NoCache);

    // Retrieves the PDF
    byte[] PDFContent = BL.PDFGenerator.GetPDF(context.Request.QueryString["DocNumber"]);

    // Send it back to the client
    context.Response.ClearHeaders();
    context.Response.ContentType = "application/pdf";
    context.Response.BinaryWrite(PDFContent);
    context.Response.Flush();
    context.Response.End();

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


Sign In to post your comments