Hello folks.
I was looking for a way to export my Telerik Chart image for use in documents and for printing, but didn't find a clean "Telerik solution" to the problem. The chart components features the Save(..) method, allowing us to save the generated image to the web-server. However, this is not what I wanted - I want to serve the image directly to the user as a download without writing it to the filesystem on the server.
Fortunately it is possible to save the image to a MemoryStream, so the following few lines does exactly what I need:
The browser will now pop up with the download dialog, allowing the user to either open or save the file.
If you want to have the image displayed in the browser, simply comment out the following line:
this.Page.Response.AddHeader("Content-disposition", "attachment; filename=Chart.png");
Hopefully this tip will be useful to someone else :)
Telerik team: Wouldn't it be possible to implement the code above in an Export function, allowing less expirenced users to do export without worrying about memory streams, response headers and so on?
Best regards
Jimmy Thomsen
I was looking for a way to export my Telerik Chart image for use in documents and for printing, but didn't find a clean "Telerik solution" to the problem. The chart components features the Save(..) method, allowing us to save the generated image to the web-server. However, this is not what I wanted - I want to serve the image directly to the user as a download without writing it to the filesystem on the server.
Fortunately it is possible to save the image to a MemoryStream, so the following few lines does exactly what I need:
System.IO.MemoryStream ms = new System.IO.MemoryStream(); |
this.chart.Save(ms, System.Drawing.Imaging.ImageFormat.Png); |
this.Page.Response.Clear(); |
this.Page.Response.ClearHeaders(); |
this.Page.Response.AddHeader("Content-disposition", "attachment; filename=Chart.png"); |
this.Page.Response.AddHeader("Content-type", "image/png"); |
this.Page.Response.BinaryWrite(ms.ToArray()); |
this.Page.Response.End(); |
The browser will now pop up with the download dialog, allowing the user to either open or save the file.
If you want to have the image displayed in the browser, simply comment out the following line:
this.Page.Response.AddHeader("Content-disposition", "attachment; filename=Chart.png");
Hopefully this tip will be useful to someone else :)
Telerik team: Wouldn't it be possible to implement the code above in an Export function, allowing less expirenced users to do export without worrying about memory streams, response headers and so on?
Best regards
Jimmy Thomsen