Actualmente estoy usando una HttpResponse para descargar archivos de mi servidor. Ya tengo un par de funciones que se utilizan para descargar archivos de Excel/Word, pero tengo problemas para descargar mi archivo de texto simple (.txt).Response.TransmitFile No descargando y no lanzando errores
Con el archivo de texto básicamente estoy volcando contenido de un TextBox en un archivo, intentando descargar el archivo con HttpResponse y luego borrando el archivo de texto temporal.
Aquí es un ejemplo de mi código que funcione para los documentos de Excel/Word:
protected void linkInstructions_Click(object sender, EventArgs e)
{
String FileName = "BulkAdd_Instructions.doc";
String FilePath = Server.MapPath("~/TempFiles/BulkAdd_Instructions.doc");
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "application/x-unknown";
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
response.TransmitFile(FilePath);
response.Flush();
response.End();
}
Y aquí es el trozo de código que no funciona.
Tomando en cuenta que el código se ejecuta sin lanzar ningún error. El archivo se crea y borra, pero nunca se descarga al usuario.
protected void saveLog(object sender, EventArgs e)
{
string date = DateTime.Now.ToString("MM_dd_yyyy_hhmm"); // Get Date/Time
string fileName = "BulkLog_"+ date + ".txt"; // Stitch File Name + Date/Time
string logText = errorLog.Text; // Get Text from TextBox
string halfPath = "~/TempFiles/" + fileName; // Add File Name to Path
string mappedPath = Server.MapPath(halfPath); // Create Full Path
File.WriteAllText(mappedPath, logText); // Write All Text to File
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "text/plain";
response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
response.TransmitFile(mappedPath); // Transmit File
response.Flush();
System.IO.File.Delete(mappedPath); // Delete Temporary Log
response.End();
}
¿Cómo lo resolvió? Estoy teniendo exactamente el mismo problema. Mi ModalpopupExtender/UpdatePanel evita que mi botón que inicia mi descarga de archivos funcione. Cuando muevo ese botón fuera de mi modalpopupextender/updatepanel, funciona sin problemas. – JoeManiaci
Gracias por informarnos que lo solucionó y no ayudó con cómo. – Danrex
Decirnos CÓMO lo arreglaste habría sido agradable – Nevyn