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枚举enum
    冒泡排序、选择排序、插入排序、二分法排序、快速排序、二叉树排序、堆排序总结
    Django-tinymce富文本的使用
    Redis-基本操作总结
    git-总结大全
    css-总结
    html-table布局
    html表单示例
    html总结
    python-浅拷贝、深拷贝实例以及讲解
  • 原文地址:https://www.cnblogs.com/liqingwen/p/6905612.html
Copyright © 2011-2022 走看看