I have the need to include a highcharts chart in a PDF report. But how can I get the image/png generated by the export.highcharts.com ? That's what I have done so far:
At a button click, this ajax request is fired:
$.ajax({
url: "Home/Teste",
type: "POST",
dataType: "html",
data: { svgParam: myChart.getSVG() },
success: function (data) {
doStuff(data);
}});
At the server, I get the request and handle as follow:
[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();
}
}
The svgParam is an html string with content like this: "
I get this svgParam in the asp.net with no problems. But the response from the export.highcharts.com is always the same, as if te svg was not sent:
<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>
For the sake of testing, I created another method in my application, to receive this WebRequest as follow:
[HttpPost]
[ValidateInput(false)]
public void Teste2(string filename, string type, string width, string svg)
{
string whereIsMySvg = svg;
}
The filenam, type and width parameters are received. But the svg one is null. I tried to encode, to not encode, serialize as json string, change the content-type...and nothing, the svg parameter never get to the destination.
Any ideas?
If you copied and pasted your code and this is not a typographical error you mispelled "svg" in the request parameters being sent to the exporting server. You have "sgv".