在网络中 Api 2 RDLC 报告控制器为什么我不能 return 到文件?
In web Api 2 RDLC report controller why can't i return to FILE?
我想做的是在 mvc web API 2 中生成一个 PDF/Excel 报告并在浏览器的新选项卡中查看。
我的错误是“不可调用成员 'File' 不能像方法一样使用”。
我是不是错过了命名空间或程序集?
非常感谢任何帮助。
这是我的代码:
'''
public IHttpActionResult 测试报告()
{
var fromDate = "01/01/2000";
var toDate = DateTime.Now.ToShortDateString();
var id = "PDF";
var result = new ApiResult();
var lr = new LocalReport();
var path = Path.Combine(HttpContext.Current.Server.MapPath("~/Reports"), "Test.rdlc");
if (System.IO.File.Exists(path))
{
lr.ReportPath = path;
}
else
{
result.Status = ApiResult.ApiStatus.Error;
result.Message = "Report Error";
return Ok(result);
}
if (fromDate == "" && toDate == "")
{
fromDate = "01/01/2000";
toDate = DateTime.Now.ToShortDateString();
}
var from = Convert.ToDateTime(fromDate);
var to = Convert.ToDateTime(toDate);
var Staffs = db.Staffs.Where(s =>
DbFunctions.TruncateTime(s.EffectiveDate) >= from.Date
&& DbFunctions.TruncateTime(s.EffectiveDate) <= to.Date && s.IsActive == true).OrderByDescending(s => s.Id)
.ToList();
var Staffs = db.Staffs.Where(s => s.empId == "10001").OrderByDescending(s => s.Id).ToList();
var Items = Staffs.Select(item => new DepViewModel
{
empId = item.empId,
Name = item.Name,
IdCard = item.IdCard,
})
.ToList();
var param0 = new ReportParameter("fromdate", from.ToShortDateString());
var param1 = new ReportParameter("todate", to.ToShortDateString());
lr.SetParameters(new ReportParameter[] { param0, param1 });
var ds = new ReportDataSource("DepDataSet1", Items.OrderByDescending(a => a.Id));
lr.DataSources.Add(ds);
string reportType = id;
string mimeType;
string encoding;
string fileNameExtension;
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>" + id + "</OutputFormat>" +
" <PageWidth>11in</PageWidth>" +
" <PageHeight>8.5in</PageHeight>" +
" <MarginTop>0.5in</MarginTop>" +
" <MarginLeft>0.5in</MarginLeft>" +
" <MarginRight>0.5in</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);
return File(renderedBytes, mimeType);
} '''
FileContentResult 是 MVC 操作结果。您没有在 WebAPI 中归档 (FileContentResult)。
请按如下方式修改您的最后一个 return 语句(return File(renderedBytes, mimeType)
):
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(renderedBytes)
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue(mimeType); //"application/pdf"
var input = $"inline; filename={fileNameExtension}"; //where fileNameExtension something with file name and extension for e.g., myfilename.txt
ContentDispositionHeaderValue contentDisposition;
if (ContentDispositionHeaderValue.TryParse(input, out contentDisposition))
{
result.Content.Headers.ContentDisposition = contentDisposition;
}
return this.ResponseMessage(result);
我想做的是在 mvc web API 2 中生成一个 PDF/Excel 报告并在浏览器的新选项卡中查看。
我的错误是“不可调用成员 'File' 不能像方法一样使用”。
我是不是错过了命名空间或程序集?
非常感谢任何帮助。
这是我的代码:
''' public IHttpActionResult 测试报告() {
var fromDate = "01/01/2000";
var toDate = DateTime.Now.ToShortDateString();
var id = "PDF";
var result = new ApiResult();
var lr = new LocalReport();
var path = Path.Combine(HttpContext.Current.Server.MapPath("~/Reports"), "Test.rdlc");
if (System.IO.File.Exists(path))
{
lr.ReportPath = path;
}
else
{
result.Status = ApiResult.ApiStatus.Error;
result.Message = "Report Error";
return Ok(result);
}
if (fromDate == "" && toDate == "")
{
fromDate = "01/01/2000";
toDate = DateTime.Now.ToShortDateString();
}
var from = Convert.ToDateTime(fromDate);
var to = Convert.ToDateTime(toDate);
var Staffs = db.Staffs.Where(s =>
DbFunctions.TruncateTime(s.EffectiveDate) >= from.Date
&& DbFunctions.TruncateTime(s.EffectiveDate) <= to.Date && s.IsActive == true).OrderByDescending(s => s.Id)
.ToList();
var Staffs = db.Staffs.Where(s => s.empId == "10001").OrderByDescending(s => s.Id).ToList();
var Items = Staffs.Select(item => new DepViewModel
{
empId = item.empId,
Name = item.Name,
IdCard = item.IdCard,
})
.ToList();
var param0 = new ReportParameter("fromdate", from.ToShortDateString());
var param1 = new ReportParameter("todate", to.ToShortDateString());
lr.SetParameters(new ReportParameter[] { param0, param1 });
var ds = new ReportDataSource("DepDataSet1", Items.OrderByDescending(a => a.Id));
lr.DataSources.Add(ds);
string reportType = id;
string mimeType;
string encoding;
string fileNameExtension;
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>" + id + "</OutputFormat>" +
" <PageWidth>11in</PageWidth>" +
" <PageHeight>8.5in</PageHeight>" +
" <MarginTop>0.5in</MarginTop>" +
" <MarginLeft>0.5in</MarginLeft>" +
" <MarginRight>0.5in</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);
return File(renderedBytes, mimeType);
} '''
FileContentResult 是 MVC 操作结果。您没有在 WebAPI 中归档 (FileContentResult)。
请按如下方式修改您的最后一个 return 语句(return File(renderedBytes, mimeType)
):
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(renderedBytes)
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue(mimeType); //"application/pdf"
var input = $"inline; filename={fileNameExtension}"; //where fileNameExtension something with file name and extension for e.g., myfilename.txt
ContentDispositionHeaderValue contentDisposition;
if (ContentDispositionHeaderValue.TryParse(input, out contentDisposition))
{
result.Content.Headers.ContentDisposition = contentDisposition;
}
return this.ResponseMessage(result);