我需要将一个Highcharts图表包含在PDF报告中,并获得由export.highcharts.com生成的image/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();
}
}
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?
hiddenField
中,然后在服务器端获取它,而不是使用ajax调用和Web方法。 - Mahmoud Farahat