码迷,mamicode.com
首页 > 其他好文 > 详细

HighCharts本地下载图片

时间:2015-05-05 21:28:59      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:

使用HighCharts下载图片的时候会弹出如下的对话框

技术分享

可以看到对话框显示从export.highcharts.com下载图片。查看highchart的js文件export.js可以看到

defaultOptions.exporting={
type:‘image/png‘,
url:‘http://export.highcharts.com‘
}

可以知道下载图片的时候是通过highcharts网站下载的,在没有外网的情况下图表下载功能无法实现

修改方法:

1.添加ImageExport.aspx

2.下载svg.dll和itextsharp.dll并引用

3.在ImageExport.aspx后台编写代码

4.在html的Highcharts脚本中修改exporting的url

exporting:{
filename:‘下载保存窗口显示的图片名称‘,
URL:‘ImageExport.aspx‘
}

 

ImageExport.aspx代码

技术分享
protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //判断参数是否正确
                //type是可以自己指定的导出类型
                //svg是默认传递的
                //filename是可以自己指定的文件名
                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";
                    }
                    //将svg转换为二进制流
                    //MemoryStream tData = new MemoryStream(Encoding.ASCII.GetBytes(tSvg));
                    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
                                {
                                    // First step saving png that would be used in pdf
                                    tSvgObj.Draw().Save(tStream, ImageFormat.Png);
                                    // Creating pdf document
                                    tDocumentPdf = new Document(new iTextSharp.text.Rectangle((float)tSvgObj.Width, (float)tSvgObj.Height));
                                    // setting up margin to full screen image
                                    tDocumentPdf.SetMargins(0.0f, 0.0f, 0.0f, 0.0f);
                                    // creating image
                                    iTextSharp.text.Image tGraph = iTextSharp.text.Image.GetInstance(tStream.ToArray());
                                    tGraph.ScaleToFit((float)tSvgObj.Width, (float)tSvgObj.Height);

                                    tStream = new MemoryStream();
                                    // Insert content
                                    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;
                        }                
                       //保存图表路径 可以自己指定
                        tFileName = Server.MapPath("~/ImageLoad/") + tFileName + "." + tExt;
                        Response.ClearContent();
                        Response.ClearHeaders();
                        Response.ContentType = tType;
                        Response.AppendHeader("Content-Disposition","attachment;filename=chart."+tExt+"");
                        //将二进制流保存为指定路径下的具体文件    
                        System.IO.File.WriteAllBytes(tFileName, tStream.ToArray());
                        Response.BinaryWrite(tStream.ToArray());                            
                        Response.End();
                    }
                }
            }
        }
View Code

 

HighCharts本地下载图片

标签:

原文地址:http://www.cnblogs.com/ecosu/p/4480044.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!