webapi后端返回文件流

 public HttpResponseMessage GetFileFromWebApi()

 {
  try
  {
  var FilePath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/download/EditPlus64_xp85.com.zip");
  var stream = new FileStream(FilePath, FileMode.Open);
  HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
  response.Content = new StreamContent(stream);
  response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
  response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") {
  FileName="Wep Api Demo File.zip"
  };
  return response;
  }
  catch
  {
  return new HttpResponseMessage(HttpStatusCode.NoContent);
  }
 }
posted @ 2021-09-10 17:53  哈根达斯  阅读(700)  评论(0编辑  收藏  举报