Wednesday, 31 July 2013

Code for downloading large amount of data file >5MB


protected void Download(object sender, EventArgs e)
    {
        string filename = Convert.ToString(((LinkButton)sender).CommandArgument);
        string filepath = "../Content/Files/";
        filepath = filepath + "" + filename;
        string extension = System.IO.Path.GetExtension(filename).ToString();

        if (filename != "")
        {
            if (extension == ".doc" || extension == ".docx")
            {
                if (File.Exists(Server.MapPath(filepath)))
                {
                    if (extension == ".docx")
                    {
                        Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                    }
                    else
                    {
                        Response.ContentType = "application/msword";
                    }
                    Response.AddHeader("content-disposition", "attachment;filename=" + filename);
                    Response.WriteFile(filepath);
                    Response.End();
                    Response.Flush();
                }
            }
            else
            {
                if (File.Exists(Server.MapPath(filepath)))
                {
                    Response.ClearContent();
                    Response.ClearHeaders();
                    Response.ContentType = "application/pdf";
                    Response.AddHeader("Content-Disposition", "attachment; filename=" +
                    filename);
                    Response.WriteFile(filepath);
                    Response.End();
                }
            }
        }
    }

No comments:

Post a Comment