Как скачать отчет RDL c в формате PDF, а не в ZIP-файл, используя метод HttpResponseMessage в WebAPI - PullRequest
0 голосов
/ 06 января 2020
public HttpResponseMessage ShowInvoiceReport(long JCompletedID, string DownloadType)
{
    FileHttpResponseMessage result = new FileHttpResponseMessage(HttpStatusCode.NoContent);
    HttpResponseMessage httpResponseMessage = Request.CreateResponse(HttpStatusCode.OK);
    string currentZipFilePath = string.Empty;
    // string DownloadType="PDF";

    try
    {
        DoctorController dcObj = new DoctorController();
        InvoicePara prObj = new InvoicePara() { JCompletedID = JCompletedID, };
        DataSet ds = dcObj.GetJobInvoiceWise(prObj);

        if (GeneralClass.IsValidDataSet(ds))
        {
                RDLCReport.ExportType reportExportType = RDLCReport.ExportType.PDF;
                reportExportType = RDLCReport.ExportType.PDF;

                //if (DownloadType == "PDF")
                //{
                //    reportExportType = RDLCReport.ExportType.PDF;
                //}
                //else if (DownloadType == "DOC")
                //{
                //    reportExportType = RDLCReport.ExportType.WORD;
                //}

                RDLCReport rdlcObj = new RDLCReport(RDLCReport.ReportFor.JobInvoice, reportExportType);

                // Profile
                List<ReportParameter> rptParas = new List<ReportParameter>();
                rptParas.Add(new ReportParameter("DocumnetRDLCPath", System.Configuration.ConfigurationManager.AppSettings["DocumnetRDLCPath"]));
                // rptParas.Add(new ReportParameter("DocumentRDLCFilePath", System.Configuration.ConfigurationManager.AppSettings["DocumentRDLCFilePath"]));

                byte[] mybytes = rdlcObj.ExportReport(ds, rptParas);

                //string mimeType;
                //return File(mybytes, mimeType, "StudentReport.pdf");

                if (mybytes != null && mybytes.Length > 0)
                {
                    string FileName = Guid.NewGuid().ToString() + ".zip";

                    string folderPath = Path.Combine(APIGlobal.FilesRootPath, "ZipToExport");
                    if (!Directory.Exists(folderPath)) 
                        Directory.CreateDirectory(folderPath);

                    currentZipFilePath = Path.Combine(folderPath, FileName);

                    #region Zipping

                    // Creates a new zip file
                    using (ZipStorer zip = ZipStorer.Create(currentZipFilePath, "Doctor Profile Details"))
                    {
                        // some operations with zip object
                        zip.EncodeUTF8 = true;

                        Stream stream = new MemoryStream(mybytes);
                        //result.Content = new StreamContent(stream);
                        //result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                        //{
                        //    FileName = "JobInvoice.pdf"
                        //};
                        //result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/.pdf");

                        zip.AddStream(ZipStorer.Compression.Deflate, "JobInvoice." + DownloadType.ToLower(), stream, DateTime.Now, "Job Invoice");

                        //CRB, PUBLICATION, AUDIT, PRESENTATION, SUBSCRIPTIONS, DOCUMENTS, OTHER DOCUMENTS
                        //int[] tableNos = { 5, 8, 9, 10, 14, 16, 17 };

                        ////Zipping FILES
                        //foreach (int tableNo in tableNos)
                        //{
                        //    foreach (DataRow dr in ds.Tables[tableNo].Rows)
                        //    {
                        //        if (Convert.ToString(dr["FilePath"]) != "")
                        //        {
                        //            string filePath = Convert.ToString(dr["FilePath"]);
                        //            string fileName = filePath.Substring(2);
                        //            string fullFilePath = System.Web.Hosting.HostingEnvironment.MapPath(filePath);
                        //            if (File.Exists(fullFilePath)) zip.AddFile(ZipStorer.Compression.Deflate, fullFilePath, fileName, "");
                        //        }
                        //    }
                        //}
                    }

                    #endregion

                    result = rdlcObj.GetHTTPResponse(currentZipFilePath, FileName, true);
                }
            }
        }
        catch (System.Threading.ThreadAbortException)
        {
        }
        catch (Exception objEx)
        {
            CommonController.AddErrorLog(objEx);
            result = new FileHttpResponseMessage(HttpStatusCode.InternalServerError);
        }
        finally
        {
        }

        return result;
}
...