Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET WebRequest to Highcharts export server
    text
    copied!<p>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:</p> <p>At a button click, this ajax request is fired:</p> <pre><code>$.ajax({ url: "Home/Teste", type: "POST", dataType: "html", data: { svgParam: myChart.getSVG() }, success: function (data) { doStuff(data); }}); </code></pre> <p>At the server, I get the request and handle as follow:</p> <pre><code>[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}&amp;type={1}&amp;width={2}&amp;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(); } } </code></pre> <p>The svgParam is an html string with content like this: " <p>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:</p> <pre><code>&lt;body&gt; &lt;div id="top"&gt; &lt;a href="http://www.highcharts.com" title="Highcharts Home Page" id="logo"&gt;&lt;img alt="Highcharts Home Page" src="resources/Highcharts-icon-160px.png" border="0"&gt;&lt;/a&gt; &lt;h1&gt;Highcharts Export Server&lt;/h1&gt; &lt;/div&gt; &lt;div id="wrap"&gt; &lt;h3&gt;Oops..,&lt;/h3&gt; &lt;p&gt;The manadatory svg POST parameter is undefined.&lt;/p&gt; &lt;/div&gt; &lt;/body&gt; </code></pre> <p>For the sake of testing, I created another method in my application, to receive this WebRequest as follow:</p> <pre><code>[HttpPost] [ValidateInput(false)] public void Teste2(string filename, string type, string width, string svg) { string whereIsMySvg = svg; } </code></pre> <p>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.</p> <p>Any ideas?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload