Ich habe eine asp.net-Webformular-Anwendung, um Videodaten aus der im Format varbinary
gespeicherten Datenbank abzurufen und als html5-Video-Tag anzuzeigen.Stream.WriteAsync wirft Der Remote-Host hat die Verbindungsausnahme geschlossen.
nach einem googled es, ich einen Weg gefunden, dass ich es asynchron ASP.Net WebApi
mit spielen, es funktioniert gut
Erstes Problem
Wenn Video erstes Mal und den Benutzer, klicken Sie auf Play-Taste gespielt Wiederholen Sie das Video, The remote host closed the connection. The error code is 0x800704CD
Ausnahme wirft bei Zeile await outputStream.WriteAsync(buffer, 0, bytesRead);
.
Zweites Problem
Wenn Benutzer klicken Sie auf Suchleiste, um das Video vom ersten bis zum gespielt gehen.
HINWEIS
Internet Explorer 11 spielt das Video ohne Probleme, aber Firefox und Chrome haben beide Probleme.
Wie kann ich dieses Problem lösen?
meine Codes hier:
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
nach auf diese Weise nicht richtig gearbeitet hat, hatte ich this way zu verwenden, aber die neue Art und Weise hat seekbar Problem, wenn Benutzer auf Suchleiste, um Zeit zu finden, funktioniert in Chrome und FireFox nicht.