Exemplo n.º 1
0
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            try
            {
                //string fecha = DateTime.Now.ToString("yyyy-MM-dd h-mm tt").Trim() + ".bak";
                //string nombre = "BKPGenloysBD-" + fecha;
                if (!string.IsNullOrWhiteSpace(txtNombreArchivo.Text))
                {
                    string nombre = txtNombreArchivo.Text.Trim() + ".bak";
                    string ruta   = Server.MapPath(@"../../../Content/Files/" + nombre);
                    if (File.Exists(ruta))
                    {
                        File.Delete(ruta);
                    }
                    if (ServicioBackup.Respaldar(nombre, ruta, "Obs"))
                    {
                        ServicioLog.CrearLog("Backup", "Backup realizado correctamente", usuarioentidad.NombreUsuario, CORE.Helpers.ConfigSection.Default.Site.Cuit.ToString());
                        Current.Response.ContentType = "application/octet-stream";
                        Current.Response.AppendHeader("Content-Disposition", "attachment; " + "filename=" + nombre + "");
                        Current.Response.TransmitFile(Server.MapPath(@"../../../Content/Files/" + nombre));
                        Current.Response.Flush();
                        //Current.Response.End();  //Causa Exception
                        Current.Response.SuppressContent = true;
                        Current.ApplicationInstance.CompleteRequest();
                    }
                    else
                    {
                        ServicioLog.CrearLog("Backup", "Backup fallido", usuarioentidad.NombreUsuario, CORE.Helpers.ConfigSection.Default.Site.Cuit.ToString());
                        sb.Append(@"<script type='text/javascript'>");
                        //sb.Append("$('#currentdetail').modal('show');");
                        sb.Append("alert('No pudo realizarse el backup');");
                        sb.Append(@"</script>");
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
                                                                "BackupFallido", sb.ToString(), false);
                    }
                }
                else
                {
                    sb.Append(@"<script type='text/javascript'>");
                    //sb.Append("$('#currentdetail').modal('show');");
                    sb.Append("alert('Complete el campo Nombre');");
                    sb.Append(@"</script>");
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
                                                            "CompletarNombre", sb.ToString(), false);
                }
            }
            catch (Exception es)
            {
                ServicioLog.CrearLog(es, "Backup", usuarioentidad.NombreUsuario, CORE.Helpers.ConfigSection.Default.Site.Cuit.ToString());
                Response.Redirect("../../../Shared/Errores.aspx");
            }
        }
Exemplo n.º 2
0
        protected void btnUpload_Click1(object sender, EventArgs e)
        {
            //Resguardo resg = new Resguardo();
            //    resg.Usuario = (Usuario) Session["usuario"];
            //    resg.Fecha = DateTime.Now;
            //    resg.Tipo = "BackUP";
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            try
            {
                if (!string.IsNullOrWhiteSpace(txtNombreArchivo.Text))
                {
                    string nombre = txtNombreArchivo.Text.Trim() + ".bak";
                    //Directory directorio;
                    //directorio.CreateDirectory(path);

                    //C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS01\MSSQL\Backup
                    string ruta = Path.GetFullPath(@"/Program Files/Microsoft SQL Server/MSSQL14.SQLEXPRESS01/MSSQL/Backup/" + nombre);
                    //if (!Directory.Exists(ruta))
                    //{
                    //    Directory.CreateDirectory(ruta);
                    //}
                    //string nombre = "backUp-" + DateTime.Now.ToString("dd-MM-yyyy-hhmmss") + ".bak";
                    //resg.Nombre = nombre;
                    //resg.Path = path;
                    //string pathCompleto = objeto.Path + objeto.Nombre;
                    //string ruta = Path.GetFullPath(@"/Windows/Backup/" + nombre);
                    if (File.Exists(ruta))
                    {
                        File.Delete(ruta);
                    }
                    //if (ServicioBackup.Respaldar(ruta))

                    if (ServicioBackup.Respaldar(ruta))
                    {
                        ServicioLog.CrearLogEventos("Backup", "Backup realizado correctamente", usuarioentidad.Apellido, (usuarioentidad.IdUsuario).ToString());
                        //// Current.Response.ContentType = "application/octet-stream";

                        //// Current.Response.AppendHeader("Content-Disposition", "attachment; filename=\" "+nombre );

                        //// Current.Response.TransmitFile(Path.GetFullPath(@" / Program Files / Microsoft SQL Server / MSSQL14.SQLEXPRESS01 / MSSQL / Backup" + nombre));
                        ////// Current.Response.TransmitFile(Server.MapPath(@"../../Content/Files/" + nombre));
                        //// Current.Response.Flush();

                        //// Current.Response.SuppressContent = true;
                        //// Current.ApplicationInstance.CompleteRequest();
                        txtNombreArchivo.Text = string.Empty;
                    }
                    else
                    {
                        ServicioLog.CrearLogEventos("Backup", "Backup fallido", usuarioentidad.Apellido, (usuarioentidad.IdUsuario).ToString());
                        sb.Append(@"<script type='text/javascript'>");

                        sb.Append("alert('No pudo realizarse el backup');");
                        sb.Append(@"</script>");
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
                                                                "BackupFallido", sb.ToString(), false);
                    }
                }
                else
                {
                    sb.Append(@"<script type='text/javascript'>");

                    sb.Append("alert('Complete el campo Nombre');");
                    sb.Append(@"</script>");
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
                                                            "CompletarNombre", sb.ToString(), false);
                }
            }
            catch (Exception es)
            {
                ServicioLog.CrearLog(es, "Backup", usuarioentidad.Apellido, (usuarioentidad.IdUsuario).ToString());
                Response.Redirect("/Shared/ErrorAdmin.aspx");
            }
        }