1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 请求一个action 将图片的二进制字节字符串在视图页面以图片形式输出

请求一个action 将图片的二进制字节字符串在视图页面以图片形式输出

时间:2019-10-31 00:05:27

相关推荐

请求一个action 将图片的二进制字节字符串在视图页面以图片形式输出

有些时候需要将二进制图片字节在发送浏览器以图片形式显示:

下面是一些示例代码:

控制器:

1/// <summary> 2/// 将图片的二进制字节字符串在视图页面以图片形式输出 3/// </summary> 4public class HomeController : Controller 5{ 6 7 public ActionResult Test() 8 { 9 return View();10 }11 12 //方法一:13 public FileResult TestFileResult_1()14 {15 byte[] mybyte;16 using (WebClient client = new WebClient())17 {18 mybyte = client.DownloadData("/video/img/video_logo_new.gif");19 MemoryStream ms = new MemoryStream(mybyte);20 //System.Drawing.Image img;21 //img = System.Drawing.Image.FromStream(ms); 22 }23 return File(mybyte, "image/gif");24 }25 26 //方法二:27 public FileResult TestFileResult()28 {29 byte[] mybyte;30 using (WebClient client = new WebClient())31 {32 mybyte = client.DownloadData("/video/img/video_logo_new.gif");33 MemoryStream ms = new MemoryStream(mybyte);34 //System.Drawing.Image img;35 //img = System.Drawing.Image.FromStream(ms); 36 }37 return new FileContentResult(mybyte, "image/gif");38 }39 40 //方法三:41 public ActionResult TestFileContentResult()42 {43 byte[] mybyte;44 using (WebClient client = new WebClient())45 {46 mybyte = client.DownloadData("/video/img/video_logo_new.gif");47 MemoryStream ms = new MemoryStream(mybyte);48 }49 return new FileContentResult(mybyte, "image/gif");50 }51 52 //方法四:53 public ActionResult TestFile()54 {55 byte[] mybyte;56 using (WebClient client = new WebClient())57 {58 mybyte = client.DownloadData("/video/img/video_logo_new.gif");59 //MemoryStream ms = new MemoryStream(mybyte);60 //System.Drawing.Image img;61 // img = System.Drawing.Image.FromStream(ms);62 }63 return File(mybyte, "image/gif");64 }65 66 67 68 69 public ActionResult Index()70 {71 return View();72 }73}

视图(view):

1 @{ 2ViewBag.Title = "Test"; 3Layout = "~/Views/Shared/_Layout.cshtml"; 4 } 5 6 TestFile:<img src="@(Url.Action("TestFile", "Home"))"/> 7 <br/> 8 <br/> 9 TestFileContentResult:<img src="@(Url.Action("TestFileContentResult", "Home"))"/>10 11 <br/>12 <br/>13 TestFileResult:<img src="@(Url.Action("TestFileResult", "Home"))"/>14 15 <br/>16 <br/>17 TestFileResult:<img src="@(Url.Action("TestFileResult_1", "Home"))"/>

运行结果如下图所示:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。