如何下载memoryStream文件

时间:2012-11-20 13:02:58

标签: c# asp.net

我是asp.net的初学者,我使用PdfRpt创建pdf文件。我在课堂上写这段代码

namespace PdfReportSamples.CustomPriceNumber
{
    public class CustomPriceNumberPdfReport
    {
        public IPdfReportData CreatePdfReport()
        {
            using (var memoryStream = new MemoryStream())
            {
                var ii= new PdfReport().DocumentPreferences(doc =>
                {
                    doc.RunDirection(PdfRunDirection.LeftToRight);
                    doc.Orientation(PageOrientation.Portrait);
                    doc.PageSize(PdfPageSize.A4);
                    doc.DocumentMetadata(new DocumentMetadata { Author = "Vahid", Application = "PdfRpt", Keywords = "Test", Subject = "Test Rpt", Title = "Test" });
                })
           .DefaultFonts(fonts =>
           {
               fonts.Path(Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\arial.ttf",
                                 Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\verdana.ttf");
           })
           .PagesFooter(footer =>
           {
               footer.DefaultFooter(DateTime.Now.ToString("MM/dd/yyyy"));
           })
           .PagesHeader(header =>
           {
               header.DefaultHeader(defaultHeader =>
               {
                   defaultHeader.RunDirection(PdfRunDirection.LeftToRight);

               });
           })
           .MainTableTemplate(template =>
           {
               template.BasicTemplate(BasicTemplate.SilverTemplate);
           })
           .MainTablePreferences(table =>
           {
               table.ColumnsWidthsType(TableColumnWidthType.Relative);
           })
           .MainTableDataSource(dataSource =>
           {
               var listOfRows = new List<Transaction>();
               for (int i = 0; i < 200; i++)
               {
                   listOfRows.Add(new Transaction
                   {
                       Product = "Item " + i,
                       Description = "Desc. " + i,
                       SalePrice = 1000 * i
                   });
               }
               dataSource.StronglyTypedList(listOfRows);
           })

           .MainTableColumns(columns =>
           {
               columns.AddColumn(column =>
               {
                   column.PropertyName("rowNo");
                   column.IsRowNumber(true);
                   column.CellsHorizontalAlignment(HorizontalAlignment.Center);
                   column.IsVisible(true);
                   column.Order(0);
                   column.Width(1);
                   column.HeaderCell("#");
               });

               columns.AddColumn(column =>
               {
                   column.PropertyName<Transaction>(x => x.Product);
                   column.CellsHorizontalAlignment(HorizontalAlignment.Center);
                   column.IsVisible(true);
                   column.Order(1);
                   column.Width(2);
                   column.HeaderCell("Product");
               });

               columns.AddColumn(column =>
               {
                   column.PropertyName<Transaction>(x => x.Description);
                   column.CellsHorizontalAlignment(HorizontalAlignment.Center);
                   column.IsVisible(true);
                   column.Order(2);
                   column.Width(3);
                   column.HeaderCell("Description");
               });

               columns.AddColumn(column =>
               {
                   column.PropertyName<Transaction>(x => x.SalePrice);
                   column.CellsHorizontalAlignment(HorizontalAlignment.Center);
                   column.IsVisible(true);
                   column.Order(3);
                   column.Width(3);
                   column.HeaderCell("Sale Price");
                   column.ColumnItemsTemplate(template =>
                   {
                       template.CustomTemplate(new CustomPriceCell());
                   });
                   column.AggregateFunction(aggregateFunction =>
                   {
                       aggregateFunction.NumericAggregateFunction(AggregateFunction.Sum);
                       aggregateFunction.DisplayFormatFormula(obj => obj == null ? string.Empty : string.Format("{0:n0}", obj));
                   });
               });

           })
           .MainTableEvents(events =>
           {
               events.DataSourceIsEmpty(message: "There is no data available to display.");
           })
           .Export(export =>
           {
               export.ToExcel();
           })
           .Generate(data => data.AsPdfStream(memoryStream));


       }


        }
    }
}

我希望当用户点击按钮时发送此文件(进入memoryStream)进行下载。但我不知道如何编写此代码。请帮我。谢谢专家

1 个答案:

答案 0 :(得分:9)

byte[] bytes = memoryStream.GetBuffer();
Response.Buffer = true;
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=report.pdf");
Response.BinaryWrite(bytes);
Response.Flush();