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

    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:ITdosComImagesitdos.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;  
    }  
  • 相关阅读:
    java算法--循环队列
    java算法--普通队列
    java算法--稀疏数组
    HelloWorld
    css
    自定义事件并且主动触发
    数组字符串操作
    进阶路上有你我-相互相持篇之ES6里箭头函数里的this指向问题
    关于一道面试题
    异步函数回调
  • 原文地址:https://www.cnblogs.com/liqingwen/p/6905612.html
Copyright © 2011-2022 走看看