如何在报表RDLC中呈现位图属性并呈现未损坏的pdf文件

时间:2013-10-23 17:17:25

标签: asp.net reporting rdlc httpresponse

我必须在我的rdlc中渲染一个位图(使用.Net报告)然后以不同的格式(包括pdf)渲染该文件。 DataSet由SeriesItemModels对象组成,列表(列表的每个项目)都有一个属性定义为Bitmap类型! 问题是我遇到两个问题: - 似乎位图没有在rdlc中呈现 - 文件扩展名消失,下载的文件已损坏 这是Print方法(我在渲染pdf文件的动作中调用的方法)

public class StatisticsStateModels
{       
    public static void Print(List<SeriesItemModels> items, string printFormat)
    {
        ReportViewer rptViewer = new ReportViewer();
        rptViewer.ProcessingMode = ProcessingMode.Local;

        rptViewer.LocalReport.EnableExternalImages = true;
        rptViewer.LocalReport.ReportPath = @HttpContext.Current.Request.PhysicalApplicationPath + "Content/States/Statistics.rdlc";
        rptViewer.LocalReport.DataSources.Add(new ReportDataSource("StatisticsModelsDataSet", StatisticsStateModels.GetStatistics(items)));

        HttpResponse response = HttpContext.Current.Response;
        DateTime now = DateTime.Now;
        string filename = now.Year + "" + now.Month + "" + now.Day + "" + now.Hour + "" + now.Minute + "" + now.Second + "" + now.Millisecond;





        Warning[] warnings;
        string[] streamIds;
        string mimeType = string.Empty;
        string encoding = string.Empty;
        string extension = string.Empty;

        switch (printFormat.ToLower())
        {
            case "pdf":
                byte[] pdfContent = rptViewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);

                response.Clear();
                //response.ClearHeaders();
                response.Buffer = true;
                response.ContentType = mimeType;
                response.AddHeader("content-disposition", "attachment; filename=" + filename + "." + extension);
                // response.ContentType = "application/pdf";
                response.BinaryWrite(pdfContent);
                response.Flush(); // send it to the client to download
                response.End();

                break;
            default:
                break;
        }
    }
}

和DataSet方法

public static List<SeriesItemModels> GetStatistics(List<SeriesItemModels> items)
    {
        return items;
    }

最后,您有下面的SeriesItem模型

  public class SeriesItemModels
{
    public string name { get; set; }
    public List<object> data { get; set; }
    public Bitmap graph { get; set; }
    public SeriesItemModels() {
        this.name = "";
        this.graph = validBitMap();//be sure: this bitMap is external and valid!
        this.data = new List<object>();
    }
}

PS:完美正确生成validBitMap!我试着把它保存在服务器上,它有效!但是,尽管我努力,该文件仍然存在损坏。

你能告诉我如何解决这些问题吗?为什么我的文件损坏了?

谢谢

1 个答案:

答案 0 :(得分:0)

这是我做的,它完美地运作: 首先我使用ImageConverter将Bitmap图形属性更改为byte []数组(见下文)

public class SeriesItemModels{
public string name { get; set; }
public List<object> data { get; set; }
private BitMap _graph;
public byte[] graph { get{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(_bitmap, typeof(byte[]));
}
set{
    //...intentionaly ignored
 } }
    public SeriesItemModels() {
    this.name = "";
    this.graph = validBitMap();//be sure: this bitMap is external and valid!
    this.data = new List<object>();
   }
  }

由于发布请求,文件已损坏!我已将请求更改为get请求,现在文件正在正确下载(未损坏)