0

In MVC I could generate .xsl or .pdf file with no issues with File(), but with the web Api nothing is happening when the action is fired! This is what I have tried so far.

I have tried a couple of solutions in here including this one Web API and report viewer but nothing has worked for me.

 public HttpResponseMessage Export(ExportVolunteerSearchFilter searchModel)
    {

        if (searchModel.Equals(null))
        {
            return Request.CreateResponse(HttpStatusCode.BadRequest);

        }

        var volunteers = _volunteersService.ExportAllVolunteersData(searchModel);
        ReportViewer ReportViewer1 = new ReportViewer();
            ReportViewer1.SizeToReportContent = true;
            ReportViewer1.LocalReport.ReportPath =

                System.Web.HttpContext.Current.Server.MapPath("~/Reports/VolunteersReport.rdlc"); 

        ReportViewer1.LocalReport.EnableExternalImages = true;
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportDataSource _rsource = new ReportDataSource("DataSet1", volunteers);
            ReportViewer1.LocalReport.DataSources.Add(_rsource);
            ReportViewer1.LocalReport.Refresh();
        Warning[] warnings;
        string[] streamIds;
        string mimeType = string.Empty;
        string encoding = string.Empty;
        string extension = string.Empty;
        string fileName = "reportVolunteer";


        HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);

        var stream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("~/Reports/VolunteersReport.rdlc"), FileMode.Open);
        response.Content = new StreamContent(stream);
        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
        response.Content.Headers.ContentDisposition.FileName = fileName;
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/xls");

        return response;

    }
sasuri
  • 972
  • 3
  • 11
  • 26

1 Answers1

-1

I have done it as:-

response.Content = new PushStreamContent(
 async (outstream) =>
{
  await getDataMethod(outstream)
},
 new MediaTypeHeadrerValue(mediaType:"application/xls"));

response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = $"test.xls"
            };
return response;