首页 > 解决方案 > 如何取消视频流 API 请求

问题描述

版本:ASP.NET Core API 2.1.X 与 IIS 和 windows server 2016 一起使用

我正在将视频从 URL 流式传输到目标:

public async Task <IActionResult> Get(string videoID, CancellationToken CT) 
  {
       HttpClient _client = new HttpClient();
       CT.ThrowIfCancellationRequested();
       try
       {
            HttpResponseMessage httpRespond = await _client.GetAsync(oneVideo.Uri, HttpCompletionOption.ResponseHeadersRead, CT);
            var _stream = await httpRespond.Content.ReadAsAsync<Stream>();
            return   File(_stream, "video/mp4");
       }
       catch (System.Exception ex) 
       {
        //Won't trigger when stream is canceled by user, Like close browser.
        //TODO: Cancel streams
       }

  }

我收到以下错误:

System.Net.Http.UnsupportedMediaTypeException:“没有 MediaTypeFormatter 可用于从媒体类型为“video/mp4”的内容中读取“Stream”类型的对象。

网络上的示例对于我的使用来说非常不足或过于复杂。我没有选择。

标签: asp.net-coreasp.net-core-webapi

解决方案


推荐阅读