.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:\ImageFile\2.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 = )
{
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;
}
.NET HttpGet 获取服务器文件下的图片信息 同步和异步方式处理的更多相关文章
- Ajax基础(二)--获取服务器文件
获取服务器文件相关步骤: 1.创建文件: 2.创建XMLHttpRequest对象: 3.获取文件(注意事项:1)在服务器中运行测试:2)注意编码问题,编码要统一). 3.1 获取xml文件: HTM ...
- AJAX获取服务器文件
写一个按钮,点击后在指定的div里显示本地txt文件内容 在本地新建一个test.txt,里面随便写点内容就好. <!DOCTYPE html> <html> <head ...
- 获取指定文件下的所有file文件
/** * 描述:获取所有的文件列表 * @param file * @param list * @return */ private List<File> getAllFiles(Fil ...
- 【web】之获取服务器tomcat下webapps目录路径
String realPath = req.getServletContext().getRealPath("/"); String realPathParent=(new Fil ...
- 获取bundle文件下的资源
NSBundle* bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle].resourcePath stringByAppendingPat ...
- ASP.NET获取服务器文件的物理路径
如下: string fullpath = context.Server.MapPath("hello.htm"); //得到hello.htm的全路径 string conten ...
- GET方式,获取服务器文件
package com.http.get; import java.io.FileOutputStream; import java.io.IOException; import java.io.In ...
- python 获取某个文件下的所有文件
import os files = os.listdir(load_Graph_file_path) cnt = 0 for file in files: print(file) if (os.pat ...
- 通过ftp同步服务器文件:遍历文件夹所有文件(含子文件夹、进度条);简单http同步服务器文件实例
该代码主要实现,指定ftp服务地址,遍历下载该地址下所有文件(含子文件夹下文件),并提供进度条显示:另外附带有通过http地址方式获取服务器文件的简单实例 废话不多说,直接上代码: 1.FTPHelp ...
随机推荐
- SpringBoot文件上传下载
项目中经常会有上传和下载的需求,这篇文章简述一下springboot项目中实现简单的上传和下载. 新建springboot项目,前台页面使用的thymeleaf模板,其余的没有特别的配置,pom代码如 ...
- 关于MVC框架中的Model的理解
一直以来对MVC结构中的Model层很不理解. 虽然Model层封装了一些方法,在查询和属性访问的时候提供了一些便利,但是实际使用中更多感受到的是Model层带来的限制,用起来十分别扭. 之前继承的一 ...
- IDEA中maven模块变成灰色
可能该模块被忽略,解决办法:
- [USACO18JAN]Cow at Large P
Description: 贝茜被农民们逼进了一个偏僻的农场.农场可视为一棵有 \(N\) 个结点的树,结点分别编号为 \(1,2,\ldots, N\) .每个叶子结点都是出入口.开始时,每个出入口都 ...
- IntelliJ IDEA配置Springboot2.x 通过devtools实现代码热部署,提高调试效率
1.pom.xml添加依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifa ...
- 资源从3ds max导入UE4问题
1.先删掉灯光和相机.材质命名为英文,贴图也要英文取名,不能有中文,并且必须是JPG格式.并整理好组:删掉多余的物体,例如线2.坐标归零.并把材质转换为默认材质3.选中一个组,先unground,然后 ...
- WebApi升级到2.0以后的XmlDocumentationProvider
using System; using System.Globalization; using System.Linq; using System.Reflection; using System.W ...
- 常见的git清单
我们每天使用 Git ,但是很多命令记不住. 一般来说,日常使用只要记住下图6个命令,就可以了.但是熟练使用,恐怕要记住60-100个命令. 这篇文章是从别人博客上copy重新整理出来的,作为笔记用, ...
- js,css文件更新之后,浏览器端还有缓存,久久不能消除
解决方案,每次更新之后修改下配置信息 /// <summary> /// VersionInfo 版本信息 /// </summary> public static class ...
- 优先队列重载<运算符
https://vjudge.net/problem/POJ-3190 #include<iostream> #include<cstdio> #include<queu ...