/// <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;
}