IT狗

echart asp.net 在服务器端 批量导出图表到指定文件夹

前台脚本

 //导出图表        function exportImage() {            if (array_charts.length < 1)            {                alert("请先查询要导出的图表");                return;            }            //array_charts为myChart对象纠合  var myChart = echarts.init(document.getElementById(chartId));             $(array_charts).each(function (i, item) {                 var DataURL = item.getDataURL("png");                console.log("DataURL-----" + DataURL);                            $.ajax({                    type: "post",                    url: "/Health/AMPReport/ExportImage.ashx",                     async: true,                    data:{                        action:"saveImage",                        imgData:DataURL                    },                    cache: false,                    success: function (msg) {                    }                });            });        }


 

ExportImage.ashx页面

  public void ProcessRequest(HttpContext context)        {            context.Response.ContentType = "text/plain";            string Action = context.Request["action"];            if (context.Request.Params["action"] != null && context.Request.Params["action"].ToString() == "saveImage")            {                var ImageSend = context.Request.Params["imgData"].ToString();                int returnid = 0;                ImageSend = ImageSend.Replace(" ", "+");                try                {                    string[] url = ImageSend.Split(',');                    string u = url[1];                    // Base64解码                    byte[] b = Convert.FromBase64String(u);                    string web_path = "/files_amp/img";                    string file_path = System.Web.HttpContext.Current.Server.MapPath(web_path);                     DirectoryInfo dInfo = new DirectoryInfo(file_path);                    if (!dInfo.Exists)                    {                        dInfo.Create();                    }                    ByteStreamToFile(file_path + "/"+DateTime.Now.ToString("yyMMddHHmmss")+".png", b);                }                catch (Exception e)                {                    returnid = 1;                    string str = e.ToString();                }                 context.Response.Write(returnid);                context.Response.End();            }        }        //二进制数组Byte[]天生文件        public static bool ByteStreamToFile(string createFileFullPath, byte[] streamByte)        {            if (!File.Exists(createFileFullPath))            {                FileStream fileStream = File.Create(createFileFullPath);                fileStream.Write(streamByte, 0, streamByte.Length);                fileStream.Close();                return true;            }            return false;        }        public bool IsReusable        {            get            {                return false;            }        }

此文由 IT狗 编辑,本网站所发布展示的作品/文章版权归原作者所有,任何商业用途均须联系作者!

相关推荐

评论 暂无评论