Apertura de Archivos en C# ASP.Net interactuando con base de datos SQL Server


21 de Octubre del 2020

Cordial saludo equipo de Programacion.net les escribe Hernán Martínez saludos desde Colombia, agradezco si me pueden colaborar acerca del inconveniente que tengo respecto a la apertura de archivos almacenados en una base de datos SQL server mediante el lenguaje de programación C# ASP.Net:

 

El video explicativo de mi requerimiento es el siguiente:

https://www.youtube.com/watch?v=FGSGit5GjzY&feature=youtu.be

 

Y el código es el siguiente 

 

protected void gvContact5_SelectedIndexChanged(object sender, EventArgs e) 

   {

string IdMov = Convert.ToString(gvContact5.DataKeys[gvContact5.SelectedIndex].Value.ToString());

string query = "select * from compyrifas where idmov = @IdMov";

            var cmd = new SqlCommand(query, sqlCon5);

            cmd.Parameters.AddWithValue("@IdMov", IdMov);

            var DA5 = new SqlDataAdapter(cmd);

DataTable dtbl = new DataTable();

            DA5.Fill(dtbl);

 

            if ((dtbl.Rows.Count == 0) || (dtbl.Rows[0]["doc"] == DBNull.Value))

            {

                MessageBox.Show("Favor vuelva al formulario de Compras_y_rifas e inserte el correspondiente documento en el campo con nombre --Documento_escaneado-- para subir el documento");

            }

            else

            {

   string filenm = dtbl.Rows[0]["realname"].ToString();

    var doc = (byte[])dtbl.Rows[0]["doc"];

      Response.Charset = "";    

 

                 Response.Cache.SetCacheability(HttpCacheability.NoCache);

 

  if (filenm.Trim().EndsWith(".pdf"))

{

   Response.ContentType = "".pdf", "application/pdf"";

   Response.AddHeader("Content-disposition", "attachment; filename=" + filenm);        

    Response.BinaryWrite(doc);

    Response.End();

}   

   else if (filenm.Trim().EndsWith(".HTM") || filenm.Trim().EndsWith(".HTML"))

{

               

 Response.ContentType = "".html", "application/html"";

 Response.AddHeader("Content-disposition", "attachment; filename=" + filenm);

 Response.BinaryWrite(doc);

 Response.End();

 

}

   else if (filenm.Trim().EndsWith(".TIF"))

{

  Response.ContentType = "".tiff", "application/tiff"";

  Response.AddHeader("Content-disposition", "attachment; filename=" + filenm);

  Response.BinaryWrite(doc);

  Response.End();

}

else if (filenm.Trim().EndsWith(".jpeg") || filenm.Trim().EndsWith(".JPG"))

{

   Response.ContentType = "".jpeg", "application/jpeg"";

   Response.AddHeader("Content-disposition", "attachment; filename=" + filenm);

   Response.BinaryWrite(doc);

   Response.End();

}

else if (filenm.Trim().EndsWith(".doc") || filenm.Trim().EndsWith(".docx"))

{

 

  Response.ContentType = "".doc", "application/msword"";

  Response.AddHeader("Content-disposition", "attachment; filename=" + filenm);

  Response.BinaryWrite(doc);

  Response.End();

}

 

else if (filenm.Trim().EndsWith(".xls") || filenm.Trim().EndsWith(".xlsx"))

 {

   Response.ContentType = "".xls", "application/msexcel"";

   Response.AddHeader("Content-disposition", "attachment; filename=" + filenm);

   Response.BinaryWrite(doc);

   Response.End();

}

 

else if (filenm.Trim().EndsWith(".accdb") || filenm.Trim().EndsWith(".mdb"))

 {

 

 Response.ContentType = "".accdb", "application/msAccess"";

 Response.AddHeader("Content-disposition", "attachment; filename=" + filenm);

 Response.BinaryWrite(doc);

 Response.End();

 }

else if (filenm.Trim().EndsWith(".ppt") || filenm.Trim().EndsWith(".pptx"))

  {

 

 Response.ContentType = "".ppt", "application/msPowerPoint"";

 Response.AddHeader("Content-disposition", "attachment; filename=" + filenm);

 Response.BinaryWrite(doc);

 Response.End();

 

  }

}

}

Muchas gracias por su colaboración.

 

Att. Hernán Martínez

Bogotá D.C – Colombia