0

I am trying to download a file which is generated by the code on shared folder. File is created, when i try to download the file the download code works fine but the file is not downloading on any browser. Download button is placed in asp.net Update Panel here is download code

try
{
    String contentType = String.Empty;
    FileInfo objFileInfo = null;

    this.FileMode = (DownloadFileMode)Session["FileMode"]; //DownloadFileMode.SingleFile;
    FileServerPath = ConfigurationManager.AppSettings["FileServerPath"];

    if (this.FileMode == DownloadFileMode.SingleFile)
    {
        if (Session["FileName"] != null)
        {
            FileName = Session["FileName"].ToString();
            contentType = "application/octet-stream";
            objFileInfo = new FileInfo(FileServerPath + FileName);
        }
        else
        {
            FileName = Session["SingleFile"].ToString();
            contentType = "application/octet-stream";
            objFileInfo = new FileInfo(FileServerPath + FileName);
        }
    }
    else if (this.FileMode == DownloadFileMode.ZippedFile)
    {
        FilesList = (List<String>)Session["FilesList"];
        contentType = "application/x-zip-compressed";
        if (FilesList.Count == 0)
            throw new Exception("No file to download");
        String strFilePath = String.Empty;
        String strZipPath = String.Empty;
        strZipPath = FileServerPath + "file" + CurrentUserSession.UserId.ToString() + "_" + CurrentUserSession.SessionId.ToString() + "_" + DateTime.Now.ToShortDateString().Replace("/", "-") + ".zip";
        DownloadFiles(strZipPath, FilesList);
        objFileInfo = new FileInfo(strZipPath);
    }
    else
    {
        ScriptManager.RegisterStartupScript(this.Page, typeof(String), "MessageAlert", "alert('Invalid file mode');", true);
    }

    Response.Clear();
    Response.AddHeader("Content-Disposition", "attachment; filename=" + objFileInfo.Name);
    Response.AddHeader("Content-Length", objFileInfo.Length.ToString());
    Response.ContentType = contentType;
    Response.TransmitFile(objFileInfo.FullName);
    //Response.End();
    HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch(Exception ex)
{
    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "MessageAlert", "alert('" + ex.Message + "');", false);
}
captainsac
  • 2,484
  • 3
  • 27
  • 48
Mohsin
  • 902
  • 3
  • 23
  • 49

2 Answers2

0

check this code.

    long sz = objFileInfo.Length;
    Response.ClearContent();
    Response.AddHeader("Content-Disposition", 
    string.Format("attachment; filename = {0}",System.IO.Path.GetFileName(objFileInfo)));
    Response.AddHeader("Content-Length", sz.ToString("F0"));
    Response.TransmitFile(objFileInfo);
    Response.End();
Genish Parvadia
  • 1,437
  • 3
  • 17
  • 30
0

I think you need to add the following trigger in your UpdatePanel.

<Triggers>
<asp:PostBackTrigger ControlID="Your_Download_Button_ID">
</Triggers>
OKEEngine
  • 888
  • 11
  • 28