コード例 #1
0
        public HttpResponseMessage Print([FromUri] long id, [FromUri] int month, [FromUri] int year, [FromUri] long socid)
        {
            try
            {
                Stream repStream       = null;
                var    documentCreator = new DocumentCreator();

                //long id = inParams.id;
                //int month = inParams.month;
                //int year = inParams.year;

                try
                {
                    var report = documentCreator.GetBillReportDocument("GetBillDetails",
                                                                       "Bill,BillingLines,FlatOwner,Society",
                                                                       System.Web.Hosting.HostingEnvironment.MapPath("~/Uploads/CR/MaintenanceReceipt.rpt"),
                                                                       socid,
                                                                       id,
                                                                       month,
                                                                       year);

                    repStream = report.ExportToStream(ExportFormatType.PortableDocFormat);
                    report.Close();
                    report.Dispose();
                }
                catch (Exception ex)
                {
                    throw new Exception("Crystal report - " + string.Format("{0}{1}", ex.Message, ex.InnerException != null ? (Environment.NewLine + ex.InnerException.Message) : string.Empty));
                }

                var        pdfname    = string.Format("{0}-{1}{2}.pdf", "Receipt", CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(month), year);
                var        filename   = "~/Uploads/MaintenanceReceipts/" + pdfname;
                FileStream fileStream = File.Create(System.Web.Hosting.HostingEnvironment.MapPath(filename), (int)repStream.Length);
                // Initialize the bytes array with the stream length and then fill it with data
                byte[] bytesInStream = new byte[repStream.Length];
                repStream.Read(bytesInStream, 0, bytesInStream.Length);
                // Use write method to write to the file specified above
                fileStream.Write(bytesInStream, 0, bytesInStream.Length);
                fileStream.Close();
                fileStream.Dispose();

                var filePath = System.Web.Hosting.HostingEnvironment.MapPath(filename);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                    {
                        byte[] bytes = new byte[file.Length];
                        file.Read(bytes, 0, (int)file.Length);
                        ms.Write(bytes, 0, (int)file.Length);

                        HttpResponseMessage httpResponseMessage = new HttpResponseMessage();
                        httpResponseMessage.Content = new ByteArrayContent(bytes.ToArray());
                        httpResponseMessage.Content.Headers.Add("x-filename", pdfname);

                        string mimeType = System.Web.MimeMapping.GetMimeMapping(pdfname);
                        httpResponseMessage.Content.Headers.ContentType                 = new MediaTypeHeaderValue(mimeType);
                        httpResponseMessage.Content.Headers.ContentDisposition          = new ContentDispositionHeaderValue("attachment");
                        httpResponseMessage.Content.Headers.ContentDisposition.FileName = pdfname;
                        httpResponseMessage.StatusCode = HttpStatusCode.OK;

                        file.Close();
                        file.Dispose();

                        return(httpResponseMessage);
                    }
                }
            }
            catch (Exception ex)
            {
                return(this.Request.CreateResponse(HttpStatusCode.InternalServerError, ex));
            }
        }