MVC3下在服务器端导出hightchats的图片

最近开发了一预算管理软件,部署在内部网络中,当需要导出hightchats生成的报表的报表的时候,出现错误,因为不能连接到export.highcharts.com

在网上查看了一些解决办法,就是在本地服务器端实现,但很多作者都没有说清楚,而且都是java,asp.net的解决办法。至于mvc3的办法,一直没有找到。

经过摸索,终于解决了。具体做法如下:

一、下载必须的第三方dll文件,用到了Svg.dll,itextsharp.dll,这些文件都可以用google查找到,都是开源的项目

二、写一个controller,添加一个方法,用来替换exporting.js的URL.我自己的类如下:

using iTextSharp.text;
using iTextSharp.text.pdf;
using Svg;
using System;
using System.ComponentModel;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Web.Mvc;  



namespace HNATC_MPS.Controllers
{
    [Description("图标导出控制器")]
    public class HightchatsController : Controller
    {
        //
        // GET: /Hightchats/

        [ValidateInput(false)]
        public void GetImgFromHightchats()
        {
            if (Request.Form["type"] != null && Request.Form["svg"] != null && Request.Form["filename"] != null)
            {
                string tType = Request.Form["type"].ToString();
                string tSvg = Request.Form["svg"].ToString();
                string tFileName = Request.Form["filename"].ToString();
                if (tFileName == "")
                {
                    tFileName = "chart";
                }
                MemoryStream tData = new MemoryStream(Encoding.UTF8.GetBytes(tSvg));
                MemoryStream tStream = new MemoryStream();
                string tTmp = new Random().Next().ToString();

                string tExt = "";
                string tTypeString = "";

                switch (tType)
                {
                    case "image/png":
                        tTypeString = "-m image/png";
                        tExt = "png";
                        break;
                    case "image/jpeg":
                        tTypeString = "-m image/jpeg";
                        tExt = "jpg";
                        break;
                    case "application/pdf":
                        tTypeString = "-m application/pdf";
                        tExt = "pdf";
                        break;
                    case "image/svg+xml":
                        tTypeString = "-m image/svg+xml";
                        tExt = "svg";
                        break;
                }

                if (tTypeString != "")
                {
                    string tWidth = Request.Form["width"].ToString();
                    Svg.SvgDocument tSvgObj = SvgDocument.Open(tData);
                    switch (tExt)
                    {
                        case "jpg":
                            tSvgObj.Draw().Save(tStream, ImageFormat.Jpeg);
                            break;
                        case "png":
                            tSvgObj.Draw().Save(tStream, ImageFormat.Png);
                            break;
                        case "pdf":
                            PdfWriter tWriter = null;
                            Document tDocumentPdf = null;
                            try
                            {
                                tSvgObj.Draw().Save(tStream, ImageFormat.Png);
                                tDocumentPdf = new Document(new Rectangle((float)tSvgObj.Width, (float)tSvgObj.Height));
                                tDocumentPdf.SetMargins(0.0f, 0.0f, 0.0f, 0.0f);
                                iTextSharp.text.Image tGraph = iTextSharp.text.Image.GetInstance(tStream.ToArray());
                                tGraph.ScaleToFit((float)tSvgObj.Width, (float)tSvgObj.Height);

                                tStream = new MemoryStream();
                                tWriter = PdfWriter.GetInstance(tDocumentPdf, tStream);
                                tDocumentPdf.Open();
                                tDocumentPdf.NewPage();
                                tDocumentPdf.Add(tGraph);
                                tDocumentPdf.CloseDocument();
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
                            finally
                            {
                                tDocumentPdf.Close();
                                tDocumentPdf.Dispose();
                                tWriter.Close();
                                tWriter.Dispose();
                                tData.Dispose();
                                tData.Close();

                            }
                            break;

                        case "svg":
                            tStream = tData;
                            break;
                    }

                    Response.ClearContent();
                    Response.ClearHeaders();
                    Response.ContentType = tType;
                    Response.AppendHeader("Content-Disposition", "attachment; filename=" + tFileName + "." + tExt + "");
                    Response.BinaryWrite(tStream.ToArray());
                    Response.End();
                }
            }
        }
    }
}

 三、在web.config修改一处配置 增加一项:<httpRuntime maxRequestLength="1048576" executionTimeout="3600" requestValidationMode="2.0"/> 如果不这样做,程序运行时,就会出现:“从客户端中检测到有潜在危险的 Request.Form 值”的提示,程序出错。

四、改写exporting.src.js,替换其中的url: 'http://export.highcharts.com/' 为url: '/Hightchats/GetImgFromHightchats'.在你的网页中,引用改写的exporting.src.js,而不是

exporting.js,当然,你也可以把改写的exporting.src.js文件压缩成exporting.js

解决办法参照了很多网友的文章和代码,在此一并感谢。

 

 

 

posted @ 2013-03-23 15:07  飞翔的天空  阅读(761)  评论(0编辑  收藏  举报