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

    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;
    }
    View Code
  • 相关阅读:
    20155328 《网络攻防》 实验一:PC平台逆向破解(5)M
    20155328 《信息安全系统设计基础》 课程总结
    构建之法
    20155327 2017-2018-2《Java程序设计》课程总结
    20155327 实验五 网络编程与安全
    20155327 网络对抗 实验
    20155327 Exp9 Web安全基础
    20155327 EXP8 Web基础
    20155327 实验四 Android程序设计
    20155327 李百乾 Exp7 网络欺诈防范
  • 原文地址:https://www.cnblogs.com/weixing/p/7490922.html
Copyright © 2011-2022 走看看