ASP.NET Core Stream/load large video files to front without having memory overflows or temp files in

Viewed 274

Cannot load video files in front with range processing in ASP.NET Core. Instead, it is reading the whole file (which is nearly 1 GB) into memory then sends it back to the browser. Code that I tried:

    public async Task<IActionResult> LoadVideo(string fileName)
    {
        var link = $"http://192.168.20.12/videos/{fileName}";

        HttpWebRequest fileReq = (HttpWebRequest) HttpWebRequest.Create(link);
        HttpWebResponse fileResp = (HttpWebResponse) fileReq.GetResponse();
        if (fileReq.ContentLength > 0)
            fileResp.ContentLength = fileReq.ContentLength;
        var stream = fileResp.GetResponseStream();


        Response.Headers.Add("Content-Transfer-Encoding", "chunked");
        Response.Headers.Add("Accept-Range", "bytes");
        Response.Headers.Add("Content-Length", fileResp.ContentLength.ToString());

        return new FileStreamResult(stream, new MediaTypeHeaderValue("video/mp4"))
        {
            EnableRangeProcessing = true,
        };
    }
0 Answers
Related