我在asp.net mvc中使用Reportviewer,并在将其转换为byte后将其呈现为pdf格式。 代码如下:
public ActionResult PrintPO(string type)
{
LocalReport lr = new LocalReport();
string path = Url.Content(Server.MapPath("~/Report/RepPurchaseOrder.rdlc"));
if (System.IO.File.Exists(path))
{
lr.ReportPath = path;
}
else
{
return Content("Report File Not Found!");
}
ReportDataSource rd = new ReportDataSource("Data", list));
lr.DataSources.Add(rd);
string reportType = type;
string mimeType;
string encoding;
string fileNameExtension;
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>" + id + "</OutputFormat>" +
" <PageWidth>10in</PageWidth>" +
" <PageHeight>10in</PageHeight>" +
" <MarginTop>0.5in</MarginTop>" +
" <MarginLeft>1in</MarginLeft>" +
" <MarginRight>1in</MarginRight>" +
" <MarginBottom>0.5in</MarginBottom>" +
"</DeviceInfo>";
Warning[] warnings;
string[] streams;
byte[] renderedBytes;
renderedBytes = lr.Render(
reportType,
deviceInfo,
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warnings);
FileContentResult fileResult = File(renderedBytes, mimeType);
return fileResult;
}
我想将此文件保存到我的服务器位置。例如:/Content/PDF/Result1.pdf
我想将渲染字节的副本复制到文件中,以便稍后我也能看到它的预览。
我怎样才能实现它?我没有使用html FileUpload控件。
请帮帮我。
感谢。
答案 0 :(得分:1)
您可以在服务器端使用FileStream
保存它。
using (FileStream fileStream = System.IO.File.Create(filePath, renderedBytes.Length)){
fileStream.Write(renderedBytes, 0, renderedBytes.Length);
}
我在方法结束时添加了在指定文件路径(/Content/PDF/Result1.pdf)中保存文件的代码,然后再设置FileContentResult
public ActionResult PrintPO(string type)
{
LocalReport lr = new LocalReport();
string path = Url.Content(Server.MapPath("~/Report/RepPurchaseOrder.rdlc"));
if (System.IO.File.Exists(path))
{
lr.ReportPath = path;
}
else
{
return Content("Report File Not Found!");
}
ReportDataSource rd = new ReportDataSource("Data", list));
lr.DataSources.Add(rd);
string reportType = type;
string mimeType;
string encoding;
string fileNameExtension;
string id="Dynamic ID Will Be Here";
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>" + id + "</OutputFormat>" +
" <PageWidth>10in</PageWidth>" +
" <PageHeight>10in</PageHeight>" +
" <MarginTop>0.5in</MarginTop>" +
" <MarginLeft>1in</MarginLeft>" +
" <MarginRight>1in</MarginRight>" +
" <MarginBottom>0.5in</MarginBottom>" +
"</DeviceInfo>";
Warning[] warnings;
string[] streams;
byte[] renderedBytes;
renderedBytes = lr.Render(
reportType,
deviceInfo,
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warnings);
//Saving renderedBytes to File ~/Content/PDF/Result1.pdf
var filesDir = Server.MapPath(@"~/Content/PDF");
if (!Directory.Exists(filesDir)) {
Directory.CreateDirectory(filesDir);
}
var filePath = Path.Combine(filesDir, "Result1.pdf");
using (FileStream fileStream = System.IO.File.Create(filePath, renderedBytes.Length)) {
fileStream.Write(renderedBytes, 0, renderedBytes.Length);
}
FileContentResult fileResult = File(renderedBytes, mimeType);
return fileResult;
}