注解
在日常中,会碰到这么些没见过,相应也有些人写过想类似的博客,但是不太全面,在我自己的日常使用中,对此加了一些修改,更加的完善。
前端脚本
$(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;
}
}