public static bool SaveAs(System.Web.HttpResponse Response, string FullFilePath) { try { FileInfo myfile = new FileInfo(FullFilePath); if (myfile.Exists) { Response.ClearContent(); Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name); Response.AddHeader("Content-Length", myfile.Length.ToString()); Response.ContentType = "application/vnd.ms-excel"; Response.WriteFile(myfile.FullName); Response.End(); } return true; } catch (ThreadAbortException) { return false; } }
/// <summary> /// Permite descargar un archivo al PC del cliente /// </summary> /// <param name="objResponse">Objeto response de la pagina desde donde se descarga el archivo</param> /// <param name="path_download">Ruta fisica del archivo</param> /// <param name="nomArchivoDescarga">Nombre con el que se decarga el archivo</param> public static void downloadFile(System.Web.HttpResponse objResponse, string pathDownload) { FileStream stream = null; BinaryReader objBinaryReader = null; Byte[] objByte = { }; if (File.Exists(pathDownload)) { try { stream = new FileStream(pathDownload, FileMode.Open, FileAccess.Read); objBinaryReader = new BinaryReader(stream); objByte = objBinaryReader.ReadBytes((int)stream.Length); objResponse.ClearHeaders(); objResponse.ClearContent(); switch (Path.GetExtension(pathDownload).ToUpper()) { case ".ZIP": objResponse.ContentType = "application/zip"; break; case ".PDF": objResponse.ContentType = "application/pdf"; break; default: objResponse.ContentType = "application/txt"; break; } objResponse.AddHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(pathDownload)); objResponse.BinaryWrite(objByte); //objResponse.WriteFile(pathDownload); objResponse.Flush(); objResponse.Close(); } catch (Exception ex) { throw ex; } } }
public static void ExportToExcel(System.Web.HttpResponse response, DataTable exportData, string exportName) { string attachment = "attachment; filename=" + exportName + ".xls"; response.ClearContent(); response.AddHeader("content-disposition", attachment); response.ContentType = "application/vnd.ms-excel"; response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1254"); response.Charset = "windows-1254"; string tab = ""; foreach (DataColumn dc in exportData.Columns) { response.Write(tab + dc.ColumnName); tab = "\t"; } response.Write("\n"); foreach (DataRow dr in exportData.Rows) { tab = ""; for (int i = 0; i < exportData.Columns.Count; i++) { response.Write(tab + (string.IsNullOrEmpty(dr[i].ToString()) ? "-" : dr[i].ToString().Replace("\t", "").Replace("\r", "").Replace("\n", ""))); tab = "\t"; } response.Write("\n"); } response.End(); }
public static void Display(System.Web.HttpResponse response, COMMON.ByteMatrix matrix, System.Drawing.Imaging.ImageFormat format, bool isAppendImage) { response.ClearContent(); response.ContentType = "image/Jpeg"; response.BinaryWrite(GetBytes(matrix, format, isAppendImage)); response.End(); }