我发现了很多示例,这些示例使用了应用程序中不可用的对象,而且似乎与我的.NET Core Web API版本不匹配。本质上,我正在一个项目上,该项目将在网页上具有标签,并且想要使用来自服务器的流而不是通过路径直接提供文件来加载视频。原因之一是文件的来源可能会更改,并且通过路径提供文件不是我的客户想要的。因此,我需要能够打开流并异步写入视频文件。
由于某种原因,这会产生JSON数据,所以这是错误的。我正在从Azure Blob存储下载视频文件并以流的形式返回,但是我只是不明白将流的视频文件发送到HTML中的标记所需的操作。
我的API控制器,
[AllowAnonymous]
[HttpGet("getintroductoryvideos")]
public async Task<Stream> GetIntroductoryVideos()
{
try
{
return _documentsService.WriteContentToStream().Result;
}
catch (Exception ex)
{
throw ex;
}
}
我的服务班,
public async Task<Stream> WriteContentToStream()
{
var cloudBlob = await _blobService.GetBlobAsync(PlatformServiceConstants._blobIntroductoryVideoContainerPath + PlatformServiceConstants.IntroductoryVideo1, introductoryvideocontainerName);
await cloudBlob.FetchAttributesAsync();
var fileStream = new MemoryStream();
await cloudBlob.DownloadToStreamAsync(fileStream);
return fileStream;
}
您可以尝试以下代码:
API控制器:
[AllowAnonymous]
[HttpGet("getintroductoryvideos")]
public async Task<FileContentResult> GetIntroductoryVideos(string videoname)
{
return await _documentsService.WriteContentToStream();
}
服务等级:
public async Task<FileContentResult> WriteContentToStream()
{
var cloudBlob = await _blobService.GetBlobAsync(PlatformServiceConstants._blobIntroductoryVideoContainerPath + PlatformServiceConstants.IntroductoryVideo1, introductoryvideocontainerName);
MemoryStream fileStream = new MemoryStream();
await cloudBlob.DownloadToStreamAsync(fileStream);
return new FileContentResult (fileStream.ToArray(), "application/octet-stream");
}
HTML:
<div className="xxx">
<video height="auto">
<source src="xx/getintroductoryvideos?videoname=xxx" type="video/mp4" />
</video>
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句