.net 中 导出echarts 图表到指定的文件夹

注解

   在日常中,会碰到这么些没见过,相应也有些人写过想类似的博客,但是不太全面,在我自己的日常使用中,对此加了一些修改,更加的完善。

前端脚本

           $(function () {
            var array_charts = new Array(myChart, myChart1)//获取myChart的集合,
            //var picBase64Info = myChart.getDataURL(); 一个myChart使用这种
                if (array_charts.length < 1)  
                {  
                    alert("请先查询要导出的图表");  
                    return;  
                }
                $(array_charts).each(function (i, item) {//这里是重点
                        var DataURL = item.getDataURL();
                    console.log("DataURL-----" + DataURL);
                    $.ajax({
                        type: "post",
                        url: "JSBGL_Service.ashx",//按自己的来
                        async: true,
                        data: {
                            action: "saveImage",
                            imgData: DataURL
                        },
                        cache: false,
                        success: function (msg) {
                        }
                    });
            });
        })
后台ashx页面

public class JSBGL_Service : IHttpHandler
{
	public void ProcessRequest(HttpContext context)
	{
		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 = "/upload";//指定存放的文件夹
                     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.Ticks.ToString() + ".png", b);//生成文件的命名  
                 }
                   
                 catch (Exception e)
                 {
                     returnid = 1;
                     string str = e.ToString();
                 }
			 }
			context.Response.ContentType = "text/plain";
            context.Response.Write(json);
            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;
        }
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值