码迷,mamicode.com
首页 > Windows程序 > 详细

【转载】C#.NET WebApi返回各种类型(图片/json数据/字符串),.net图片转二进制流或byte

时间:2017-05-25 20:41:12      阅读:946      评论:0      收藏:0      [点我收藏+]

标签:byte   json   读取   ret   href   file   path   llb   字符   

 

C#.NET WebApi返回各种类型(图片/json数据/字符串),.net图片转二进制流或byte

转载:http://www.itdos.com/Mvc/20150302/0741255.html

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;  
}  

 

【转载】C#.NET WebApi返回各种类型(图片/json数据/字符串),.net图片转二进制流或byte

标签:byte   json   读取   ret   href   file   path   llb   字符   

原文地址:http://www.cnblogs.com/liqingwen/p/6905612.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!