首页 > 代码库 > MVC、一般处理程序hanlder 输出图片文件
MVC、一般处理程序hanlder 输出图片文件
hanlder.ashx文件
[csharp] view plain copy
- public class Handler4 : IHttpHandler
- {
- public void ProcessRequest(HttpContext context)
- {
- #region 将图片转成Base64
- FileStream fs = new FileStream("I26S-1280x1024-s.jpg", FileMode.Open, FileAccess.Read);
- byte[] buffur = new byte[fs.Length];
- fs.Read(buffur, 0, (int)fs.Length);
- string strPic = Convert.ToBase64String(buffur);
- #endregion
- #region 将Base64转成byte[]
- byte[] buffurPic = Convert.FromBase64String(strPic);
- #endregion
- context.Response.ContentType = "image/jpeg";
- context.Response.Clear();
- context.Response.BufferOutput = true;
- context.Response.OutputStream.Write(buffurPic, 0, buffurPic.Length);
- context.Response.Flush();
- }
- public bool IsReusable
- {
- get
- {
- return false;
- }
- }
- }
页面文件
[html] view plain copy
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title></title>
- <script src=http://www.mamicode.com/"js/jquery.js"></script>
- </head>
- <body>
- <script language="JavaScript">
- $(document).ready(function () {
- $(‘#img1‘).attr(‘src‘, ‘/Handler/Handler4.ashx‘);
- });
- </script>
- <img id="img1" src=http://www.mamicode.com/"" />
- </body>
- </html>
MVC Action方法
[csharp] view plain copy
- public ActionResult GetImg()
- {
- #region 将图片转成Base64
- FileStream fs = new FileStream("I26S-1280x1024-s.jpg", FileMode.Open, FileAccess.Read);
- byte[] buffur = new byte[fs.Length];
- fs.Read(buffur, 0, (int)fs.Length);
- string strPic = Convert.ToBase64String(buffur);
- #endregion
- #region 将Base64转成byte[]
- byte[] buffurPic = Convert.FromBase64String(strPic);
- #endregion
- return File(buffurPic, "image/jpeg");
- }
页面文件
[html] view plain copy
- @{
- Layout = null;
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>PicShow</title>
- <script src=http://www.mamicode.com/"~/jquery/jquery.min.js"></script>
- <script language="JavaScript">
- $(document).ready(function () {
- $(‘#img1‘).attr(‘src‘, ‘@Url.Action("GetImg", "Selector")‘);
- });
- </script>
- </head>
- <body>
- <img id="img1" /><br /><br />
- <img id="img2" src="http://www.mamicode.com/@Url.Action("GetImg", "Selector")" />
- </body>
- </html>
MVC、一般处理程序hanlder 输出图片文件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。