0

i made a controller that calls the service, and ate to write 1 MB to the server Controller

  [HttpGet("Download")]
    public async Task DownloadFile()
    {

       await _dataTransfer.DownloadFile(ControllerContext);
    }

Service

 public async Task DownloadFile(ControllerContext controllerContext)
    {
        var response = controllerContext.HttpContext.Response;
        var request = controllerContext.HttpContext.Request;
        var fileStream = new FileStream(Path.Combine(_path, _fileZip), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
        var buffer = new byte[1024 * 1024]; // 1 MB buffer

        try
        {
            var fileSize = fileStream.Length;

            // Parse the range header to determine the byte range to send
            var rangeHeader = request.Headers["Range"].ToString();
            long start = 0, end = fileSize - 1;

            if (!string.IsNullOrEmpty(rangeHeader))
            {
                var range = rangeHeader.Replace("bytes=", "").Split('-');
                long.TryParse(range[0], out start);
                if (range.Length > 1 && long.TryParse(range[1], out end))
                {
                    if (end > fileSize - 1) end = fileSize - 1;
                }
            }

            // Set the Content-Length header with the length of the range to send
            var contentLength = end - start + 1;
            response.Headers.Add("Content-Length", contentLength.ToString());

            //// Set the Content-Range header with the range being sent
            //response.Headers.Add("Content-Range", $"bytes {start}-{end}/{fileSize}");

            // Set the status code to Partial Content (206)
            response.StatusCode = (int)HttpStatusCode.PartialContent;

            // Set the content type and disposition headers
            response.ContentType = "application/octet-stream";
            response.Headers.Add("Content-Disposition", $"attachment; filename={Path.GetFileName(_path)}");

            // Seek to the start of the range
            fileStream.Seek(start, SeekOrigin.Begin);

            // Send the range in 1MB chunks
            int bytesRead;
            while ((bytesRead = await fileStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
            {
                if (response.Body.CanWrite)
                {
                    await response.Body.WriteAsync(buffer, 0, bytesRead);
                    await response.Body.FlushAsync();
                }
                await _hubContext.Clients.All.SendAsync("Download", $"bytes - {bytesRead} / {fileSize}");
            }

            response.StatusCode = (int)HttpStatusCode.OK;
        }
        catch (Exception ex)
        {
            _logger.Log(LogLevel.Error, ex.Message, ex?.InnerException);
        }
        finally
        {
            fileStream.Dispose();
        }
    }

The problem is that the Response has already been sent, and it can no longer be used, I can’t find other ways to send data

Maybe there are examples of download, upload large files from the server to the front, and back

0 Answers0