gpt4 book ai didi

c# - 如何下载内存流文件

转载 作者:太空宇宙 更新时间:2023-11-03 17:54:58 25 4
gpt4 key购买 nike

我是 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 中)以供下载。但我不知道如何编写这段代码。请帮我。感谢专家

最佳答案

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();

关于c# - 如何下载内存流文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13473777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com