Click here to Skip to main content
15,884,872 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void btnDownload_Click(object sender, EventArgs e)
        {
                 string contentType = string.Empty;  
                if (ddlFileFormat.SelectedValue.Equals(".pdf"))  
                    contentType = "application/pdf";  
                if (ddlFileFormat.SelectedValue.Equals(".doc"))  
                    contentType = "application/ms-word";  
                if (ddlFileFormat.SelectedValue.Equals(".xls"))  
                    contentType = "application/xls";
                string empid = Session["Empid"].ToString();
                DataTable dsData = new DataTable();
                objOldIncBOL.EmpId = Session["Empid"].ToString();
                dsData = objOldIncBLL.getdata(objOldIncBOL);     
                string FileName = "File_" + DateTime.Now.ToString("ddMMyyyyhhmmss") + ddlFileFormat.SelectedValue;  
                string extension;  
                string encoding;  
                string mimeType;  
                string[] streams;  
                Warning[] warnings;    
                LocalReport report = new LocalReport();
                report.ReportPath = Server.MapPath("~\\UI\\OldRecords\\OldIncrementReport.rdlc");  
                ReportDataSource rds = new ReportDataSource();
                rds.Name = "OldIncrementReport";//This refers to the dataset name in the RDLC file  
                rds.Value = dsData;  
                report.DataSources.Add(rds);
                Byte[] mybytes = report.Render(ddlFileFormat.SelectedItem.Text, null,  
                                out extension, out encoding,  
                                out mimeType, out streams, out warnings); 
            //for exporting to PDF

                var a = mybytes;
               
                using (FileStream fs = File.Create(Server.MapPath("~/download/") + FileName))  
                {  
                     fs.Write(mybytes, 0, mybytes.Length);
                    
                }

                string fileNames = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ddlFileFormat.SelectedValue;

                //fileattch.InnerText = ;
                //string fileName = this.Id.ToString() + extension;
                //switch (AppConfiguration.DataStoreType)
                //{
                //    case DataStoreType.Database:
                //        myFile.Save();
                //        break;
                //    case DataStoreType.FileSystem:
                //        myFile.Save(Server.MapPath(Path.Combine(
                //        AppConfiguration.UploadsFolder, myFile.FileUrl)));
                //        break;
                //}
               

                Response.ClearHeaders();  
                Response.ClearContent();  
                Response.Buffer = true;  
                Response.Clear();  
                Response.ContentType = contentType;  
                Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);  
                Response.WriteFile(Server.MapPath("~/download/" + FileName));
                Response.BinaryWrite(mybytes);
                Response.Flush();  
                Response.Close();  
                Response.End();


        }
Posted
Updated 20-Apr-15 17:52pm
v5
Comments
Andy Lanng 20-Apr-15 10:24am    
Note that I have wrapped your code in 'pre' tags. These options can be found at the top of the text area.
Could you flesh this one out a bit please. Which filepath can you not save in a variable? Why can't you just store it in a variable. Please use the 'Improve Question' button above this comment to add some detail to your request.

Thanks ^_^

1 solution

If you are worried about losing the value you could store it in a hidden field on the page or save it as session variable. If you use a hidden field on the page make sure you set the view state property of the control so that they value is not lost on post back.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900