Example #1
0
    protected void btn_pdf_click(object sender, EventArgs e)
    {
        PreRutaBC gd       = new PreRutaBC();
        String    mimeType = "";

        try
        {
            // DataTable excel = gd.CrearEnvio(hseleccionado.Value.ToString(), user.USUA_ID, chk_archivar.Checked);
            // ViewState["lista"] = excel;

            //    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "exp", "exportar();", true);


            if (hseleccionado.Value == "")
            {
                utils.ShowMessage(this, "Seleccione al menos un viaje", "error", false);
                return;
            }


            this.pnlReport.Visible = true;
            ReportBC report = new ReportBC();
            //      VIAJEBC v = new VIAJEBC().ObtenerXID(Convert.ToInt32(this.tbidviajed.Text));
            List <int> ids = hseleccionado.Value.ToString().Split(',').Select(int.Parse).ToList();

            string zip      = GenerateFileNamezipPDF("hr_", ".zip");
            int    contador = 0;


            if (ids.Count == 1)
            {
                try
                {
                    // DataTable excel = gd.CrearEnvio(hseleccionado.Value.ToString(), user.USUA_ID, chk_archivar.Checked);
                    // ViewState["lista"] = excel;

                    //    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "exp", "exportar();", true);

                    this.pnlReport.Visible = true;
                    DataTable        datos      = report.obrenerReporteDespachoViaje(ids[0].ToString());
                    ReportDataSource dataSource = new ReportDataSource("Datos", datos);

                    this.ReportViewer1.LocalReport.DataSources.Clear();
                    this.ReportViewer1.LocalReport.DataSources.Add(dataSource);



                    Warning[] warnings;
                    string[]  streamids;

                    string encoding;
                    string extension;
                    //Word
                    byte[] bytes = this.ReportViewer1.LocalReport.Render(
                        "PDF", null, out mimeType, out encoding,
                        out extension,
                        out streamids, out warnings);
                    //byte[] renderedBytes = this.ReportViewer1.LocalReport.Render("PDF");
                    this.Response.Clear();

                    this.Response.ContentType = mimeType;

                    this.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}{1}{2}", datos.Rows[0]["numero"].ToString(), '.', extension));

                    this.Response.BinaryWrite(bytes);

                    this.Response.End();
                }
                catch (Exception ex)
                {
                    utils.ShowMessage(this, ex.Message, "error", false);
                }
                finally
                {
                    ObtenerRutas(true);
                }
            }
            else
            {
                while (contador < ids.Count)
                {
                    DataTable        datos      = report.obrenerReporteDespachoViaje(ids[contador].ToString());
                    ReportDataSource dataSource = new ReportDataSource("Datos", datos);

                    this.ReportViewer1.LocalReport.DataSources.Clear();
                    this.ReportViewer1.LocalReport.DataSources.Add(dataSource);
                    Warning[] warnings;
                    string[]  streamids;
                    string    encoding;
                    string    extension;
                    //Word
                    byte[] bytes = this.ReportViewer1.LocalReport.Render(
                        "PDF", null, out mimeType, out encoding,
                        out extension,
                        out streamids, out warnings);
                    //byte[] renderedBytes = this.ReportViewer1.LocalReport.Render("PDF");

                    Stream stream = new MemoryStream(bytes);


                    UtilsWeb.AddStreamToZip(zip, stream, GenerateFileNamezipPDF("hr_" + datos.Rows[0]["numero"].ToString(), ".pdf"));
                    contador = contador + 1;
                }



                //this.Response.Clear();
                //this.Response.ContentType = mimeType;
                //this.Response.AddHeader("content-disposition", string.Format("attachment; filename=Hoja_Ruta_{0}{1}{2}", 'a', '.', extension));
                //this.Response.BinaryWrite(bytes);
                //this.Response.End();


                Response.Clear();
                Response.ContentType = mimeType;
                Response.AddHeader("content-disposition", "attachment; filename=" + "descarga_multiple.zip");
                Response.BinaryWrite(File.ReadAllBytes(zip));
                //   File.Delete(Server.MapPath("./cargadefotos/Output.zip"));
                File.Delete(zip);

                Response.End();
            }
        }
        catch (Exception ex)
        {
            utils.ShowMessage(this, ex.Message, "error", false);
        }
        finally
        {
            ObtenerRutas(true);
        }
    }