I have an asp.net webforms application and to extract the video from a database saved in varbinary format and show it as a html5 video tag.
after searching google, I found a way that I have to play asynchronously using ASP.Net WebApi , it works fine
First problem
When the video is played for the first time, and the user presses the play button to play the video, The remote host closed the connection. The error code is 0x800704CD The remote host closed the connection. The error code is 0x800704CD exception The remote host closed the connection. The error code is 0x800704CD in the string await outputStream.WriteAsync(buffer, 0, bytesRead); .
Second problem
When the user clicks on the search bar, the video is first played.
Note
Internet Explorer 11 plays the video without any problems, but firefox and chrome have both problems.
how can i solve this problem?
Here are my codes:
public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.EnableCors(); config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "VideoApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } } public class VideoController : ApiController { public IVideoRepository videoRepository; public HttpResponseMessage Get(long id) { try { videoRepository = new VideoRepository(); Video video = videoRepository.load(id); if (video != null) { var videoStream = new VideoStream(video.fileContent); string ext = video.extension; var response = Request.CreateResponse(); response.Content = new PushStreamContent((Action<Stream, HttpContent, TransportContext>)videoStream.WriteToStream, new MediaTypeHeaderValue("video/" + ext)); response.Content.Headers.Add("Content-Disposition", "attachment;filename=" + video.fullName.Replace(" ", "")); response.Content.Headers.Add("Content-Length", videoStream.FileLength.ToString()); return response; } else { return Request.CreateResponse(HttpStatusCode.NotFound); } } catch (Exception e) { return Request.CreateErrorResponse(HttpStatusCode.ServiceUnavailable, e); } } } public class VideoStream { private readonly byte[] _fileContent; private long _contentLength; public long FileLength { get { return _contentLength; } } public VideoStream(byte[] content) { _contentLength = content.Length; _fileContent = content; } public async void WriteToStream(Stream outputStream, HttpContent content, TransportContext context) { try { var buffer = new byte[65536]; MemoryStream memoryStream = new MemoryStream(); memoryStream.Write(_fileContent, 0, _fileContent.Length); memoryStream.Position = 0; using (memoryStream) { var length = (int)memoryStream.Length; var bytesRead = 1; while (length > 0 && bytesRead > 0) { bytesRead = memoryStream.Read(buffer, 0, Math.Min(length, buffer.Length)); await outputStream.WriteAsync(buffer, 0, bytesRead); length -= bytesRead; } } } catch (Exception e) { throw e; } finally { outputStream.Close(); } } }
UPDATE
after that it didn’t work properly, I had to use it this way , but the new method has a problem with the search when the user clicks on the search bar to search if he doesn’t work in Chrome and FireFox.