.net rest api 下载文件代码

[HttpGet]
public HttpResponseMessage GetImage()
{
  //文件路径
  var path = HostingEnvironment.MapPath("~/App_Data/5.jpg");
  FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
  Image img = Image.FromStream(fileStream);
  MemoryStream ms = new MemoryStream();
  img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
  HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
  result.Content = new ByteArrayContent(ms.ToArray());
  result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");
  result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
  {
      FileName = "5.jpg"//文件名
  };
  return result;
}

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注