使用ReportViewer导出的Blazor(WASM) [英] Blazor (Wasm) with ReportViewer export

查看:12
本文介绍了使用ReportViewer导出的Blazor(WASM)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照此视频的说明操作:https://www.youtube.com/watch?v=7V0Yb5drLgQ

当链接直接放入我的剃须刀的组件html部分时,一切正常:

RazorComponent

<a type="button" style="margin-left:20px;" class="btn btn-outline-secondary" href="https://localhost:5001/api/Report/GetReport?reportType=1">
        <span class="oi oi-data-transfer-download"></span>&nbsp;&nbsp;@(l.Keys["CPVH_Download_PDF"])
    </a>
</div>

下面是我的API的控制器逻辑

服务器Api

   [HttpGet("[action]/{reportType}")]
    public IActionResult GetReport(int reportType)
    {
        var dt = new DataTable();
        dt = employeeService.GetEmployee();

        string mimeType = "";
        int extension = 1;
        var path = $"{this._webHostEnvironment.WebRootPath}\Reports\Report1.rdlc";

        Dictionary<string, string> parameters = new Dictionary<string, string>();

        parameters.Add("param", "RDLC report in Blazor Web Assembly");

        LocalReport localReport = new LocalReport(path);
        
        localReport.AddDataSource("dsEmployee", dt);

        //For Pdf
        if (reportType == 1)
        {
            var result = localReport.Execute(RenderType.Pdf, extension, parameters, mimeType);
            return File(result.MainStream, "application/pdf", "myPdf.pdf");
        }
        else //if (reportType == 1)
        {
            //For Excel
            var result = localReport.Execute(RenderType.Excel, extension, parameters, mimeType);
            return File(result.MainStream, "application/msexcel", "myReport.xls");
        }
    }
但是,当我将剃须刀组件调用包装到调用GET或POST的服务中时,即使我仍然点击控制器,文件也不会下载。我只收到OK响应,但没有下载文件。

调用API的服务

  public async Task GetReport(int reportType)
    {
        var httpResponse = await _httpClient.GetAsync($"{_httpClient.BaseAddress.ToString()}/Report/GetReport/{reportType}");

        var saveDir = Path.Combine(Path.GetTempPath(), "myPdf.pdf");

        if (httpResponse.IsSuccessStatusCode)
        {
            var content = httpResponse.Content.ReadAsByteArrayAsync().Result;
            File.WriteAllBytes(saveDir, content);
        }
    }

推荐答案

我已经找到了使用第三方扩展解决此问题的方法:https://github.com/arivera12/BlazorDownloadFile

_iBlazorDownloadFileService.DownloadFile获取fileBytes并将其转换为保存在下载文件夹中的文件。

这里是我的实现

服务

 public async Task<byte[]> GetReport(int reportType)
 {
    var httpResponse = await _httpClient.GetAsync($"{_httpClient.BaseAddress.ToString()}/Report/GetReport/{reportType}");
    byte[] content = null;

    if (httpResponse.IsSuccessStatusCode)
        content = httpResponse.Content.ReadAsByteArrayAsync().Result;

    return content;
 }

Razor.Component

@inject IExportService _iExportService
@inject IBlazorDownloadFileService  _iBlazorDownloadFileService 
      
<a style="margin-left:20px;" class="btn btn-outline-secondary" @onclick="@(async ()=> await GetExport((int)ExportTypesEnum.Pdf))"  >
            <span class="oi oi-data-transfer-download"></span>&nbsp;&nbsp;@(l.Keys["CPVH_Download_PDF"])
        </a>
@code {
    public async Task GetExport(int reportType)
    {
        var fileBytes = await _iExportService.GetReport(reportType);
                
        await _iBlazorDownloadFileService.DownloadFile("myPdf.pdf", fileBytes, "application/pdf");

    }
}

这篇关于使用ReportViewer导出的Blazor(WASM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆