zoukankan      html  css  js  c++  java
  • C#.NET WebApi返回各种类型(图片/json数据/字符串)

    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;
    }
  • 相关阅读:
    KNN分类算法补充
    KNN分类算法实现手写数字识别
    KNN分类算法及python代码实现
    数据挖掘与机器学习介绍
    安装Numpy方法
    windows下的python环境搭建(python2和python3不兼容,python2用的多)
    用户画像知识
    Mahout介绍和简单应用
    协同过滤的实现步骤
    推荐系统基础知识
  • 原文地址:https://www.cnblogs.com/lintaicheng/p/13952719.html
Copyright © 2011-2022 走看看