我需要在 PDF 报告中包含一个 highcharts 图表。但是如何获得 export.highcharts.com 生成的图像/png?这就是我到目前为止所做的:
单击按钮时,将触发此 ajax 请求:
$.ajax({
url: "Home/Teste",
type: "POST",
dataType: "html",
data: { svgParam: myChart.getSVG() },
success: function (data) {
doStuff(data);
}});
在服务器上,我收到请求并按如下方式处理:
[HttpPost]
[ValidateInput(false)]
public void Teste(string svgParam)
{
// Create a request using a URL that can receive a post.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://export.highcharts.com/");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = string.Format("filename={0}&type={1}&width={2}&sgv={3}", "chart", "image/png", 1270, Server.UrlEncode(svgParam));
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded; multipart/form-data";
//User agent is based in a normal export.js request
request.UserAgent = @"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
//This is here just to read the response.
string msg;
using (StreamReader sReader = new StreamReader(webResponse.GetResponseStream()))
{
msg = sReader.ReadToEnd();
}
}
svgParam 是一个 html 字符串,内容如下:"
我在 asp.net 中得到这个 svgParam 没有问题。但是 export.highcharts.com 的响应总是一样的,就好像 te svg 没有发送一样:
<body>
<div id="top">
<a href="http://www.highcharts.com" title="Highcharts Home Page"
id="logo"><img alt="Highcharts Home Page"
src="resources/Highcharts-icon-160px.png" border="0"></a>
<h1>Highcharts Export Server</h1>
</div>
<div id="wrap">
<h3>Oops..,</h3>
<p>The manadatory svg POST parameter is undefined.</p>
</div>
</body>
为了测试,我在我的应用程序中创建了另一个方法来接收这个 WebRequest,如下所示:
[HttpPost]
[ValidateInput(false)]
public void Teste2(string filename, string type, string width, string svg)
{
string whereIsMySvg = svg;
}
接收文件名、类型和宽度参数。但是 svg 是空的。我尝试编码,不编码,序列化为 json 字符串,更改内容类型......什么都没有,svg 参数永远不会到达目的地。
有任何想法吗?