Response End en Update Panel

Se tenia un boton que generaba un archivo txt segun items seleccionados en una grilla. La grilla estaba dentro de un update panel.

Para generar el archivo, se generaba en memoria y luego se volcaba a un txt finalizando en response.end()

El response.end(), es incompatible con las llamadas asincronas por lo que el <asp:AsyncPostBackTrigger ControlID=»ImgBtnDescargar» EventName=»Click» /> no funciona.

Para que se realice la accion de generar el archivo se tuvo que cambiar  <asp:PostBackTrigger ControlID=»ImgBtnDescargar» />, sin embargo se generaba y descargaba el archivo pero no se actualizaba la grilla.

 

Al final se ha optado por generar el archivo en una ruta del servidor y redirigir la aplicacion a una nueva pagina que descarga el archivo.

 

private string GenerarArchivo()
{
string NombreArchivo = System.IO.Path.GetRandomFileName();
string NombreGenerado = "CargaSUNAT_" + DateTime.Now.ToString("yyyyMMdd");

System.IO.MemoryStream stream=new System.IO.MemoryStream();
//System.IO.StreamWriter SW = new System.IO.StreamWriter(stream);
System.IO.StreamWriter SW = new System.IO.StreamWriter(Server.MapPath("txtGenerados/" + NombreGenerado + ".txt"));

BLLDocument _BllDocument = new BLLDocument();

foreach (GridViewRow fila in this.gvDocumentosBaja.Rows)
{
DataKey dkey = gvDocumentosBaja.DataKeys[fila.RowIndex];
int IdCorrelativo = int.Parse(dkey[0].ToString());
CheckBox chkSeleccionado = (CheckBox)fila.Cells[0].FindControl("chkselbaja");
if (chkSeleccionado.Checked)
{
BEDocument _documento = _BllDocument.DocumentoTXT(IdCorrelativo);
string [] numeroDoc=_documento.NumberDocument.Split('-');
SW.WriteLine(_documento.ClientRUC + "|" + _documento.DateDocument.ToString("yyyyMMdd") + "|" + _documento.TipoDocumentoDescripcion +
"|" + numeroDoc[0] + "|" + numeroDoc[1] + "|" + _documento.DateDocument.ToString("yyyyMMdd") + "|" + _documento.MensajeBaja);

BEMethExResultInfo resultado = _BllDocument.RegistraTXTGenerado(IdCorrelativo, _SessionVariable.BEUser.Name);

}
}

SW.Close();

return NombreGenerado;
}

 

 protected void ImgBtnDescargar_Click(object sender, ImageClickEventArgs e)
        {
            string NombreGenerado=GenerarArchivo();
            //Response.Redirect("GenerarTXT.aspx");

            if (chkverTodos.Checked)
                CargarGrilla("1");
            else
                CargarGrilla("0");
            
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AbrirDescarga", string.Format("window.open('Descargas.aspx?Fileid={0}');",NombreGenerado), true); 
        }

 

    public partial class Descargas : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["Fileid"] != null)
                {
                    string nombrearchivo = Request.QueryString["Fileid"].ToString();

                    System.IO.FileStream fs = null;
                    fs = System.IO.File.Open(Server.MapPath("txtGenerados/" + nombrearchivo + ".txt"), System.IO.FileMode.Open);
                    byte[] txtbyte = new byte[fs.Length];
                    fs.Read(txtbyte, 0, Convert.ToInt32(fs.Length));
                    fs.Close();
                    Response.AddHeader("Content-disposition", "attachment; filename=" + nombrearchivo + ".txt");
                    Response.ContentType = "application/octet-stream";
                    Response.BinaryWrite(txtbyte);
                    Response.End();
                }

            }
        }
    }