/// <summary> /// 输出Jsonp文本到客户端 /// </summary> /// <param name="Response"></param> /// <param name="callBack">客户端的js回调方法</param> /// <param name="json">json参数</param> public static void WriteJsonp(this HttpResponseBase Response, string callBack, IJson json) { Response.ContentType = "application/x-javascript"; Response.ClearContent(); Response.BinaryWrite(Encoding.UTF8.GetBytes(string.Format("{0}({1});", callBack, json.ToString()))); Response.Flush(); }
/// <summary> /// 输出纯文本到客户端 /// </summary> /// <param name="Response"></param> /// <param name="text"></param> public static void WriteText(this HttpResponseBase Response, string text) { Response.ContentType = "text/plain"; Response.ClearContent(); Response.BinaryWrite(Encoding.UTF8.GetBytes(text)); Response.Flush(); }
/// <summary> /// 输出Json文本到客户端 /// </summary> /// <param name="Response"></param> /// <param name="json"></param> public static void WriteJson(this HttpResponseBase Response, IJson json) { Response.ContentType = "application/json"; Response.ClearContent(); Response.BinaryWrite(Encoding.UTF8.GetBytes(json.ToString())); Response.Flush(); }
/// <summary> /// Checks for authorization failure and if result of ajax call overrides asp.net redirect to return a 401. /// </summary> /// <param name="request">The request.</param> /// <param name="response">The response.</param> /// <param name="context">The context.</param> internal void CheckForAuthorizationFailure(HttpRequestBase request, HttpResponseBase response, HttpContextBase context) { if (!request.IsAjaxRequest() || !true.Equals(context.Items["RequestWasNotAuthorized"])) return; response.StatusCode = 401; response.ClearContent(); var content = new {title = HttpContext.GetGlobalResourceObject("Session", "Title.SessionHasExpired"), content = HttpContext.GetGlobalResourceObject("Session", "Messsage.SessionHasExpired")}; var serializer = new JavaScriptSerializer(); response.Write(serializer.Serialize(content)); }
internal void CheckForAuthFailure(HttpRequestBase request, HttpResponseBase response, HttpContextBase context) { //NOTE: This will change *all* 302's for AJAX requests to 401s. That could cause problems, but it will // work for now. if (response.StatusCode == 302 && request.IsAjaxRequest()) { response.StatusCode = 401; response.ClearContent(); } }
private void ResponseImg(HttpResponseBase response, string pic) { try { string FilePath = AppDomain.CurrentDomain.BaseDirectory; FilePath = Path.Combine(FilePath, "App_Data\\" + imgpath); string FileFullPath = Path.Combine(FilePath, pic); string FileExt = Path.GetExtension(FileFullPath); if (U.ExtValid(FileExt)) { if (File.Exists(FileFullPath)) { FileExt = FileExt.ToLower(); Image Img = Image.FromFile(FileFullPath); ImageFormat format = ImageFormat.Jpeg; switch (FileExt) { case ".gif": format = ImageFormat.Gif; break; case ".jpg": format = ImageFormat.Jpeg; break; case ".png": format = ImageFormat.Png; break; default: break; } response.ClearContent(); response.ContentType = "image/bmp"; Img.Save(response.OutputStream, format); Img.Dispose(); response.Flush(); } else { response.Write("file DO NOT Exists"); } } else { response.Write("file DO NOT Allow"); } } catch (Exception ex) { log.Warn(ex.Message); log.Warn("imgpath:{0},imgname:{1}", imgpath, imgname); } }
private void SetHttpResponse(HttpResponseBase httpResponse, string fileNameWithoutExtension, ExcelPackage package) { httpResponse.ClearContent(); httpResponse.Buffer = true; //Write it back to the client httpResponse.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; httpResponse.AddHeader("content-disposition", string.Format("attachment; filename={0}.xlsx", fileNameWithoutExtension)); httpResponse.BinaryWrite(package.GetAsByteArray()); httpResponse.Flush(); httpResponse.End(); }
public void FileDownload(HttpResponseBase response,string filePah) { if(!IsExists(filePah)) { throw new Exception("�ļ�������"); } var fileInfo = new FileInfo(filePah); response.Clear(); response.ClearContent(); response.ClearHeaders(); response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileInfo.Name,System.Text.Encoding.UTF8)); response.AddHeader("Content-Length", fileInfo.Length.ToString()); //response.AddHeader("Content-Transfer-Encoding", "binary"); response.ContentType = "application/vnd.ms-excel;charset=UTF-8"; response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); response.WriteFile(fileInfo.FullName); response.Flush(); response.End(); }
public static void DumpXls(HttpResponseBase response, DataTable table, string fileName) { var grid = new GridView(); grid.DataSource = table; grid.DataBind(); response.ClearContent(); response.Buffer = true; response.AddHeader("content-disposition", string.Format("attachment; filename={0}.xls", fileName)); response.ContentType = "application/ms-excel"; response.Charset = ""; var sw = new StringWriter(); var htw = new HtmlTextWriter(sw); grid.RenderControl(htw); response.Output.Write(sw.ToString()); response.Flush(); response.End(); }
private void CreateCheckCodeImage(string checkCode, HttpResponseBase response) { if (checkCode == null || checkCode.Trim() == String.Empty) return; System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 16.5)), 35); Graphics g = Graphics.FromImage(image); try { //生成随机生成器 Random random = new Random(); //清空图片背景色 g.Clear(Color.White); // Pen drawPen = new Pen(Color.Blue); // 添加多种颜色 hooyes Color[] colors = { Color.Blue,Color.Silver,Color.SlateGray,Color.Turquoise,Color.Violet,Color.Turquoise,Color.Tomato,Color.Thistle,Color.Teal,Color.SteelBlue }; //画图片的背景噪音线 for (int i = 0; i < 9; i++) { int x1 = random.Next(image.Width); int x2 = random.Next(image.Width); int y1 = random.Next(image.Height); int y2 = random.Next(image.Height); Pen drawPen2 = new Pen(colors[i]); g.DrawLine(drawPen2, x1, y1, x2, y2); } // drawPen.Dispose(); Tahoma Font font = new System.Drawing.Font("Arial", 13, (System.Drawing.FontStyle.Bold)); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Black, Color.Gray, 1.2f, true); g.DrawString(checkCode, font, brush, 2, 1); // g.DrawString("J", font, brush, 1, 115); font.Dispose(); brush.Dispose(); //画图片的前景噪音点 for (int i = 0; i < 20; i++) { int x = random.Next(image.Width); int y = random.Next(image.Height); image.SetPixel(x, y, Color.FromArgb(0x8b, 0x8b, 0x8b)); } //画图片的边框线 Pen borderPen = new Pen(Color.Transparent); g.DrawRectangle(borderPen, 0, 0, image.Width - 1, image.Height - 1); borderPen.Dispose(); System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); byte[] buffer = ms.ToArray(); ms.Dispose(); response.ClearContent(); response.ContentType = "image/bmp"; response.BinaryWrite(buffer); } finally { g.Dispose(); image.Dispose(); } }
/// <summary> /// Exports the chart to the specified HttpResponse object. This method /// is preferred over WriteToStream() because it handles clearing the /// output stream and setting the HTTP reponse headers. /// </summary> /// <param name="httpResponse"></param> public void WriteToHttpResponse(HttpResponseBase httpResponse) { httpResponse.ClearContent(); httpResponse.ClearHeaders(); httpResponse.ContentType = this.ContentType; httpResponse.AddHeader("Content-Disposition", this.ContentDisposition); WriteToStream(httpResponse.OutputStream); }
/// <summary> /// 生成位图,输出到响应流 /// </summary> /// <param name="Response"></param> /// <param name="Code"></param> public static void GetCode(HttpResponseBase Response, out string Code) { Code = string.Empty; Bitmap bit = GetCode(out Code); ////清除该页输出缓存,设置该页无缓存 //Response.Buffer = true; //Response.ExpiresAbsolute = DateTime.Now.AddMilliseconds(0); //Response.Expires = 0; //Response.CacheControl = "no-cache"; //Response.AppendHeader("Pragma", "No-Cache"); Response.ClearContent(); bit.Save(Response.OutputStream, ImageFormat.Png); Response.ContentType = "image/png"; //释放资源 bit.Dispose(); }
private static void GenerarPdf7(DataTable dt, string titulo, string nombreDoc, EmpresaDTO objEmpresa, DateTime? FechaFin, HttpResponseBase Response) { GridView gv = new GridView(); gv.DataSource = dt; gv.AllowPaging = false; gv.DataBind(); if (dt.Rows.Count > 0) { PintarCabeceraTabla(gv); //PintarIntercaladoCategorias(gv); AddSuperHeader(gv, titulo + " - Empresa:" + objEmpresa.Nombre); //Cabecera principal AddWhiteHeader(gv, 1, ""); AddWhiteHeader(gv, 2, "FECHA LIMITE: " + FechaFin.GetValueOrDefault().ToShortDateString()); //PintarCategorias(gv); Response.ClearContent(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment; filename=" + nombreDoc + "_" + objEmpresa.Nombre + "_" + DateTime.Now.ToString("dd-MM-yyyy") + ".xls"); Response.ContentType = "application/ms-excel"; Response.Charset = ""; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); gv.RenderControl(htw); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); htw.Close(); sw.Close(); } }
protected override void WriteFile(HttpResponseBase response) { response.BufferOutput = false; response.Clear(); response.ClearContent(); response.ClearHeaders(); response.Cookies.Clear(); response.ContentType = ContentType; response.ContentEncoding = Encoding.Default; response.AddHeader("Content-Type", ContentType); response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", this.FileDownloadName)); byte[] buffer = new byte[4096]; using (ZipOutputStream zipOutputStream = new ZipOutputStream(response.OutputStream)) { foreach (string fileName in FilesToZip) { int folderOffset = fileName.LastIndexOf('\\'); Stream fs = System.IO.File.OpenRead(fileName); // or any suitable inputstream string entryName = fileName.Substring(folderOffset); ZipEntry entry = new ZipEntry(ZipEntry.CleanName(entryName)); entry.Size = fs.Length; // Setting the Size provides WinXP built-in extractor compatibility, // but if not available, you can set zipOutputStream.UseZip64 = UseZip64.Off instead. zipOutputStream.PutNextEntry(entry); //zipOutputStream.SetLevel(3); // 0-9 for compression level //zipOutputStream.Password(); // security int count = fs.Read(buffer, 0, buffer.Length); while (count > 0) { zipOutputStream.Write(buffer, 0, count); count = fs.Read(buffer, 0, buffer.Length); if (!response.IsClientConnected) { break; } response.Flush(); } fs.Close(); } zipOutputStream.Finish(); } //response.OutputStream.Flush(); response.End(); }
private void ExportToExcel(string exportContent, HttpResponseBase response) { response.Clear(); response.ClearContent(); response.AddHeader("content-disposition", "attachment;filename=" + fileName); response.Charset = ""; response.ContentType = "application/vnd.xls"; System.IO.StringWriter stringWrite = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); response.Write(exportContent); response.End(); }