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;
    }
  • 相关阅读:
    left join
    order by 对null的处理
    checkbox不显示,试试去掉-webkit-appearance这个样式
    浅谈ES6的let和const的异同点
    ES6中箭头函数的作用
    HTML页面每次打开的时候都清除页面缓存
    解决HTML加载时,外部js文件引用较多,影响页面打开速度问题
    JQuery和Zepto的差异(部分)
    vue-router 快速入门
    vue-resource插件使用
  • 原文地址:https://www.cnblogs.com/lintaicheng/p/13952719.html
Copyright © 2011-2022 走看看