• .NET HttpGet 获取服务器文件下的图片信息 同步和异步方式处理


            /// <summary>
            /// 项目文件夹下路径  返回流类型数据,如:图片类型
            /// </summary>
            /// <returns></returns>
            public HttpResponseMessage GetImg()
            {  //  ~/ ImageFile / 001.png
                //项目文件夹下路径
                var imgPath = System.Web.Hosting.HostingEnvironment.MapPath("~/ImageFile/2.jpg");
               // 从图片中读取byte
                var imgByte = File.ReadAllBytes(imgPath);
               // 从图片中读取流
                var imgStream = new MemoryStream(File.ReadAllBytes(imgPath));
                var resp = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StreamContent(imgStream)
                    //或者
                  //  Content = new ByteArrayContent(imgByte)
                };
                resp.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");
                return resp;
            }
     [HttpGet]
            public HttpResponseMessage SelectUploadFile()
            {
    
                Image img = Image.FromFile(@"C:ImageFile2.jpg");
    
                MemoryStream ms = new MemoryStream();
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
                result.Content = new ByteArrayContent(ms.ToArray());
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png"); //设置http响应上的Content-Type 为image/Png媒体类型
    
                return result;
    
            }

     异步操作获取文件图片

     [Route("Main")]
            [HttpGet]
            public async Task<HttpResponseMessage> GetMain(string Ordernumber, int seq = 1)
            {
                return await Task.FromResult<HttpResponseMessage>(GetFileImage("main", Ordertnumber, seq));
            }
    
     private HttpResponseMessage GetFileImage(string type, string Ordernumber, int seq)
            {
                string basePath = @"C:ImageFile";
                string filePath = basePath + type + "\_" + type + "_" + seq.ToString() + ".png";
    
                Image img = Image.FromFile(filePath);
                MemoryStream ms = new MemoryStream();
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
                result.Content = new ByteArrayContent(ms.ToArray());
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/Png");
                return result;
            }

     

  • 相关阅读:
    如何系统收集网页/电子书等相关的知识点
    针式PKM软件多个版本兼容功能的应用
    软件开发出路:做精某一卖点!
    深入针式PKM应用系列(1) 虚拟文件夹功能,让文件想放哪就放哪
    PKM软件:提供知识应用的工具支持
    解决长串英文字母显示不能自动换行的问题
    C#中判断字符串A中是否包含字符串B
    小技巧锦集
    解决Notes的"The remote server is not a known tcp/IP host"问题的方法
    将Sql Server自增长字段的目前识别值重调!
  • 原文地址:https://www.cnblogs.com/Warmsunshine/p/9853560.html
Copyright © 2020-2023  润新知