Recently, a budget management software was developed and deployed in the internal network. When you need to export the report generated by hightchats, an error occurs because it cannot be connected to export.highcharts.com.
I checked some solutions on the Internet, that is, implementation on the local server side, but many authors have not made it clear, and they are all Java and Asp.net solutions. The mvc3 method has not been found.
After exploration, we finally solved the problem. The procedure is as follows:
1. Download required third-party DLL files and use SVG. dll and itextsharp. dll. These files can be found by Google and are all open-source projects.
2. Write a controller and add a method to replace the exporting. js URL. My own class is as follows:
Using itextsharp. text; using itextsharp.text.pdf; using SVG; using system. componentmodel; using system. drawing. imaging; using system. io; using system. text; using system. web. MVC; namespace hnatc_mps.controllers {[description ("")] public class hightchatscontroller: controller {// get:/hightchats/ [Validateinput (false)] Public void getimgfromhightchats () {If (request. Form ["type"]! = NULL & request. Form ["SVG"]! = NULL & request. Form ["FILENAME"]! = NULL) {string tType = request. form ["type"]. tostring (); string tsvg = request. form ["SVG"]. tostring (); string tfilename = request. form ["FILENAME"]. tostring (); If (tfilename = "") {tfilename = "chart";} memorystream tdata = new memorystream (encoding. utf8.getbytes (tsvg); memorystream tstream = new memorystream (); string ttmp = new random (). next (). tostring (); string text = ""; string ttypestring = ""; Switch (tType) {Case "image/PNG": ttypestring = "-M image/PNG"; text = "PNG"; break; case "image/JPEG": ttypestring = "-M image/JPEG"; text = "jpg"; break; Case "application/pdf ": ttypestring = "-M application/pdf"; text = "pdf"; break; Case "image/SVG + XML": ttypestring = "-M image/SVG + XML "; TEXT = "SVG"; break;} If (ttypestring! = "") {String twidth = request. form ["width"]. tostring (); SVG. svgdocument tsvgobj = svgdocument. open (tdata); Switch (text) {Case "jpg": tsvgobj. draw (). save (tstream, imageformat. JPEG); break; Case "PNG": tsvgobj. draw (). save (tstream, imageformat. PNG); break; Case "pdf": Export writer twriter = NULL; document tdocumentpdf = NULL; try {tsvgobj. draw (). save (tstream, imageformat. PNG); tdocumentpdf = new document (New rectangle (float) tsvgobj. width, (float) tsvgobj. height); tdocumentpdf. setmargins (0.0f, 0.0f, 0.0f, 0.0f); itextsharp. text. image tgraph = itextsharp. text. image. getinstance (tstream. toarray (); tgraph. scaletofit (float) tsvgobj. width, (float) tsvgobj. height); tstream = new memorystream (); twriter = memory writer. getinstance (tdocumentpdf, tstream); tdocumentpdf. open (); tdocumentpdf. newpage (); tdocumentpdf. add (tgraph); tdocumentpdf. closedocument ();} catch (exception ex) {Throw ex;} finally {tdocumentpdf. close (); tdocumentpdf. dispose (); twriter. close (); twriter. dispose (); tdata. dispose (); tdata. close ();} break; Case "SVG": tstream = tdata; break;} response. clearcontent (); response. clearheaders (); response. contenttype = tType; response. appendheader ("content-disposition", "attachment; filename =" + tfilename + ". "+ TEXT +" "); response. binarywrite (tstream. toarray (); response. end ();}}}}}
III,Add one item to modify a configuration in Web. config: Requestvalidationmodel = "2.0 "/> If you do not do this,ProgramWhen running, the following error occurs: "request. form value detected from the client.
4. Rewrite exporting. SRC. JS, replace the URL 'HTTP: // export.highcharts.com/'with the URL:'/hightchats/getimgfromhightchats '. in your web page, reference the rewrittenExporting. SRC. jsInstead
Exporting. js. Of course, you can rewriteExporting. SRC. jsFile compressionExporting. js
The solution is based onArticleAndCode.