public async Task GenerateReportFile([FromQuery] DateTime dateFrom, [FromQuery] DateTime dateTo, [FromQuery] string tagIds, [FromQuery] string type)
        {
            var requestModel = new GenerateReportModel();
            var tags         = tagIds?.Split(",").ToList();

            if (tags != null)
            {
                foreach (string tag in tags)
                {
                    requestModel.TagIds.Add(int.Parse(tag));
                }
            }
            requestModel.DateFrom = dateFrom;
            requestModel.DateTo   = dateTo;
            requestModel.Type     = type;
            var result = await _reportService.GenerateReportFile(requestModel);

            const int bufferSize = 4086;

            byte[] buffer = new byte[bufferSize];
            Response.OnStarting(async() =>
            {
                if (!result.Success)
                {
                    Response.ContentLength = result.Message.Length;
                    Response.ContentType   = "text/plain";
                    Response.StatusCode    = 400;
                    byte[] bytes           = Encoding.UTF8.GetBytes(result.Message);
                    await Response.Body.WriteAsync(bytes, 0, result.Message.Length);
                    await Response.Body.FlushAsync();
                }
                else
                {
                    await using var wordStream = result.Model;
                    int bytesRead;
                    Response.ContentLength = wordStream.Length;
                    Response.ContentType   = requestModel.Type == "docx"
                        ? "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
                        : "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

                    while ((bytesRead = wordStream.Read(buffer, 0, buffer.Length)) > 0 &&
                           !HttpContext.RequestAborted.IsCancellationRequested)
                    {
                        await Response.Body.WriteAsync(buffer, 0, bytesRead);
                        await Response.Body.FlushAsync();
                    }
                }
            });
        }
Пример #2
0
        public async Task GenerateReportFile(GenerateReportModel requestModel)
        {
            OperationDataResult <FileStreamModel> result = await _reportService.GenerateReportFile(requestModel);

            const int bufferSize = 4086;

            byte[] buffer = new byte[bufferSize];
            Response.OnStarting(async() =>
            {
                try
                {
                    if (!result.Success)
                    {
                        Response.ContentLength = result.Message.Length;
                        Response.ContentType   = "text/plain";
                        Response.StatusCode    = 400;
                        byte[] bytes           = Encoding.UTF8.GetBytes(result.Message);
                        await Response.Body.WriteAsync(bytes, 0, result.Message.Length);
                        await Response.Body.FlushAsync();
                    }
                    else
                    {
                        using (FileStream excelStream = result.Model.FileStream)
                        {
                            int bytesRead;
                            Response.ContentLength = excelStream.Length;
                            Response.ContentType   = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                            while ((bytesRead = excelStream.Read(buffer, 0, buffer.Length)) > 0 &&
                                   !HttpContext.RequestAborted.IsCancellationRequested)
                            {
                                await Response.Body.WriteAsync(buffer, 0, bytesRead);
                                await Response.Body.FlushAsync();
                            }
                        }
                    }
                }
                finally
                {
                    if (!string.IsNullOrEmpty(result?.Model?.FilePath) &&
                        System.IO.File.Exists(result.Model.FilePath))
                    {
                        System.IO.File.Delete(result.Model.FilePath);
                    }
                }
            });
        }