My file downloads but not showing download dialog in browsers

I have a project, .NET Core 7 for the backend and NextJs for the frontend. Based on user’s need and requirements, there are many files (as videos with .mp4 format) in my windows VPS saved inside a folder with their unique name. When user clicks on download using their device, these videos start downloading automatically. Right now, almost everything works fine in terms of downloading video. There is a small problem that I have no idea where it is coming from. I want when a user clicks on download, the download dialog of the browser shows the file that is being downloaded. Right now, the way it works is first it downloads completely, after completion informs the user that your file is downloaded completely.
I was searching for the similar problem, I figured out that I have to add “Content-Disposition” header with “attachment” and my file name. Even though I added that to my header, still it downloads, then it shows the completed dialog.
Down below is my code:

Code for my service:

public async Task<Stream?> GetVideoStreamAsync(string videoCode, CancellationToken cancellationToken = default)
    {
        var localVideoPath = Path.Combine(BaseVideoPath, videoCode + ".mp4");
        if (File.Exists(localVideoPath))
        {
            await using var fs = new FileStream(localVideoPath, FileMode.Open, FileAccess.Read, FileShare.Read); 
            var sr = new MemoryStream();
            await fs.CopyToAsync(sr, cancellationToken);
            sr.Position = 0; // Reset the position of the MemoryStream to the start after copying
            return sr;
        }
        return null;
    }

And here is my controllers code:

[HttpGet("GetStream")]
    public async Task<IActionResult> StreamVideo([FromQuery] string videoCode, CancellationToken cancellationToken = default)
    {
        try
    {
        var filePath = Path.Combine(BaseVideoPath, videoCode + ".mp4");
        if (!System.IO.File.Exists(filePath))
        {
            return NotFound("Video not found.");
        }

        var fileInfo = new FileInfo(filePath);
        var fileLength = fileInfo.Length;
        var videoStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);

        var fileName = $"{videoCode}.mp4";
        Response.ContentType = "video/mp4";
        Response.Headers.Add("Content-Disposition", $"attachment; filename="{fileName}"");
        Response.Headers.Add("Accept-Ranges", "bytes");

        if (Request.Headers.ContainsKey("Range"))
        {
            return await ProcessRangeRequest(videoStream, fileLength, cancellationToken);
        }

        // Set the Content-Length for full downloads
        Response.ContentLength = fileLength;
        return File(videoStream, "video/mp4", enableRangeProcessing: true);
    }
    catch (Exception ex)
    {
        _logger.LogError(ex, "Failed to stream video");
        return StatusCode(StatusCodes.Status500InternalServerError);
    }
    }

    private async Task<IActionResult> ProcessRangeRequest(Stream videoStream, long actualFileSize, CancellationToken cancellationToken)
    {
        var rangeHeader = RangeHeaderValue.Parse(Request.Headers["Range"].ToString());
        var range = rangeHeader.Ranges.First();
        var start = range.From ?? 0;
        var end = range.To ?? actualFileSize - 1;

        if (start >= actualFileSize || end >= actualFileSize)
        {
            Response.Headers.Add("Content-Range", $"bytes */{actualFileSize}");
            return StatusCode(StatusCodes.Status416RequestedRangeNotSatisfiable);
        }

        var contentLength = end - start + 1;
        Response.StatusCode = StatusCodes.Status206PartialContent;
        Response.Headers.Add("Content-Range", $"bytes {start}-{end}/{actualFileSize}");
        Response.ContentLength = contentLength;

        // Position the stream and copy the range to the response body
        videoStream.Position = start;
        await videoStream.CopyToAsync(Response.Body, Convert.ToInt32(end - start + 1), cancellationToken);

        return new EmptyResult();
    }

Has anyone had the same issue before ? I would appreciate if you can help me solve this problem. Thanks

I have tried almost every header, changing the “Content-Disposition” but still, the same problem.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật