C#.NET WebApi返回各种类型(图片转二进制流或byte /json数据/字符串)
2015-07-22 17:12
645 查看
using System.IO; /// <summary> /// WebApi返回图片 /// </summary> public HttpResponseMessage GetQrCode() { var imgPath = @"D:\ITdosCom\Images\itdos.jpg"; //从图片中读取byte var imgByte = File.ReadAllBytes(imgPath); //从图片中读取流 var imgStream = new MemoryStream(File.ReadAllBytes(imgPath)); var resp = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(imgByte) //或者 //Content = new StreamContent(stream) }; resp.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpg"); return resp; } /// <summary> /// WebApi返回json数据 /// </summary> public HttpResponseMessage GetQrCode() { var jsonStr = "{\"IsSuccess\":true,\"Data\":\"www.itdos.com\"}"; var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(jsonStr, Encoding.UTF8, "text/json") }; return result; } /// <summary> /// WebApi返回字符串 /// </summary> public HttpResponseMessage GetQrCode() { var str = "IT大师www.itdos.com"; var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(str, Encoding.UTF8, "text/plain") }; return result; }
相关文章推荐
- Json是什么时候开始的,Json起源,Json历史
- Grunt 自动化部署之css、image、javascript、html压缩Gruntfile.js配置文件
- 简单的js实现网页时钟
- javascript对象(date转换/正则表达式)
- JSON.parse()方法
- [转]JavaScript文件操作(2)-FileReader
- js网页如何获取手机屏幕宽度
- javascript-DOM学习
- js效果大全
- javascript使用事件
- chart.js操作实例(前后台互通)
- Javascript异步编程之二回调函数
- Chart.js报表制作
- json-jsonConfig使用
- js快速查找字符串某个字符出现次数
- JS简单入门教程
- JS上传图片-通过FileReader获取图片的base64
- json:There is a cycle in the hierarchy!
- 使用Jsoup 抓取页面的数据
- js中,符合属性的js写法是讲下横杆去掉