Nancy 图片流

在使用 Nancy API 时 需要给web 传递一个图片,这个图片不是物理文件而是临时生成的数据流

尝试Response.AsFile(streamxxx,"image/jpg")  Response.FromStream(streamxxx,"image/jpg")  都失败前端无法展示

查询资料 给Response他增加一个扩展方法

 /// /// Byte array response/// /// Byte array to be the body of the response/// Content type to usepublic ByteArrayResponse(byte[] body, string contentType = null){this.ContentType = contentType ?? "application/octet-stream";this.Contents = stream =>{using (var writer = new BinaryWriter(stream)){writer.Write(body);}};}}/// /// 版 本/// Copyright (c) /// 创建人:/// 日 期:/// 描 述:扩展图片流传递/// public static class Extensions{public static Response FromByteArray(this IResponseFormatter formatter, byte[] body, string contentType = null){return new ByteArrayResponse(body, contentType);}}

最终 

MemoryStream ms = new MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return Response.FromByteArray(ms.ToArray(), "image/jpg");

 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部