public HttpResponseMessage Post(string version, string environment,
    string filetype)
{
    var path = @"C:\Temp\test.exe";
    HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
    var stream = new FileStream(path, FileMode.Open, FileAccess.Read);
    result.Content = new StreamContent(stream);
    result.Content.Headers.ContentType = 
        new MediaTypeHeaderValue("application/octet-stream");
    return result;
}

  

注意事项

  • 不用调用stream.Dispose() 方法去释放资源,WebAPI会将数据发送给客户端后自动调用Dispose 方法,因此不可以使用 using (var stream = …) 代码块,这样会报500
  • 必须确保流的起始位置为 0,stream.Seek(0, SeekOrigin.Begin); 或stream.Position = 0;

相关文章:

  • 2022-03-02
  • 2022-12-23
  • 2021-05-25
  • 2021-09-23
  • 2022-12-23
  • 2021-10-19
  • 2021-11-18
  • 2022-12-23
猜你喜欢
  • 2021-07-19
  • 2021-06-18
  • 2021-09-05
  • 2021-11-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案