1

我必须在我的 rdlc 中渲染一个位图(使用 .Net Reporting),然后以不同的格式(包括 pdf)渲染这个文件。DataSet 由 SeriesItemModels 对象 List 组成,它(列表的每个项目)具有一个定义为 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 生成完美正确!我试图将它保存在服务器上,它有效!但尽管我努力,文件仍然损坏。

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

谢谢

4

1 回答 1

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

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

于 2013-10-25T19:46:05.513 回答