If you do not wish the exported image to be sent back to browser, but instead want it to be saved on the server, you need to make the following changes from the previous example, where the file was downloaded:

  1. Open the server-side export handler and configure the path of write-enabled folder in which images will be saved, as well as set the HTTP URL representation of that folder in the code. The handlers also allow a few advanced configuration with respect to file naming.
  2. Change exportAction='save' in the XML
  3. Remove exportTargetWindow attribute from your XML. As there are no downloads returned, there is no question of which window to return it in
  4. Create a JavaScript method in your web page that gets called once the image has been saved on server. Specify the name of this function in exportCallback attribute of XML. If you do not specify any names here, by default, a JavaScript function named FC_Exported is called.

Let's first start with Step 1, where you configure the path of your temporary save folder on server and set the HTTP reference for the same.

 
Configuring the server-side export handlers

In the export handler, you'll need to configure the following:

  • Configure the folder on your server where the images/PDFs would be saved. Make sure that this folder has proper write permissions, as otherwise the export handlers would not be able to write to the file system. For checking and setting security settings on your server, consult the web server's documentation (i.e., IIS, Apache etc. docs)
  • You need to specify the HTTP mapping of this folder path, if you need your JavaScript functions to directly access this saved images. For example, say you saved your image on the server at c:\inetpub\wwwroot\yoursite\temp (or /users/{you}/www/temp) and the images saved here can be reached through http://www.yoursite.com/temp, you need to specify the URL "http://www.yoursite.com/temp" as mapping HTTP URL for this folder path.
  • Configure the file name settings - by default, FusionMaps export handlers uses GUID for automatic file name generation. At the end of this, you can opt to append current timestamp of server, or a random number to avoid over-riding of any files (in case of thousands of concurrent files being saved at a given second). You can also configure whether the files will be over-written in that folder or not (which is the default).

Depending on which export handler you're using, follow the steps below:

If PHP:

Open FCExporter.php, scroll down, and make the changes in following lines:

/* ----------------------- EXPORT PATH & URI -------------------------------- */

/**
* IMPORTANT: You need to change the location of folder where
* the exported chart images/PDFs will be saved on your
* server. Please specify the path to a folder with
* write permissions in the constant SAVE_PATH below.
*/

define ( "SAVE_PATH", "./" );
/* Place your folder path here.*/

/**
* IMPORTANT: This constant HTTP_URI stores the HTTP reference to
* the folder where exported charts will be saved.
* Please enter the HTTP representation of that folder
* in this constant e.g., http://www.yourdomain.com/images/
*/

define ( "HTTP_URI", "http://www.yourdomain.com/images/" );
/* Define your HTTP Mapping URL here. */

/**
* OVERWRITEFILE sets whether the export handler would overwrite an existing file
* the newly created exported file. If it is set to false the export handler would
* not overwrite. In this case if INTELLIGENTFILENAMING is set to true the handler
* would add a suffix to the new file name. The suffix is a randomly generated UUID.
* Additionally, you add a timestamp or random number as additional suffix.
*
*/

define ( "OVERWRITEFILE", false );
define ( "INTELLIGENTFILENAMING", true );
define ( "FILESUFFIXFORMAT", "TIMESTAMP" ) ;// can be TIMESTAMP or RANDOM

/* Define file over-write, auto-naming and naming suffix configuration here. */

 
If ASP.NET:
Open FusionCharts.aspx.cs and edit the following:

/* ----------------------- EXPORT PATH & URI -------------------------------- */

/**
* IMPORTANT: You need to change the location of folder where
* the exported chart images/PDFs will be saved on your
* server. Please specify the path to a folder with
* write permissions in the constant SAVE_PATH below.
*/

private const string SAVE_PATH = "./";

/**
* IMPORTANT: This constant HTTP_URI stores the HTTP reference to
* the folder where exported charts will be saved.
* Please enter the HTTP representation of that folder
* in this constant e.g., http://www.yourdomain.com/images/
*/

private const string HTTP_URI = "http://www.yourdomain.com/images/";

/**
* OVERWRITEFILE sets whether the export handler would overwrite an existing file
* the newly created exported file. If it is set to false the export handler would
* not overwrite. In this case if INTELLIGENTFILENAMING is set to true the handler
* would add a suffix to the new file name. The suffix is a randomly generated GUID.
* Additionally, you add a timestamp or a random number as additional suffix.
*
*/

private bool OVERWRITEFILE = false;
private bool INTELLIGENTFILENAMING = true;
private string FILESUFFIXFORMAT = "TIMESTAMP"; // can be TIMESTAMP or RANDOM

 
If JSP:
Open fusioncharts_export.properties file present in the Classes directory and make changes in the following values there.
#Please specify the path to a folder with write permissions relative to web application root
SAVEPATH=./images/

#This constant HTTP_URI stores the HTTP reference to
#the folder where exported maps will be saved.
#Please enter the HTTP representation of that folder
#in this constant e.g., http://www.yourdomain.com/images/

HTTP_URI=http://www.yourdomain.com/images/

#OVERWRITEFILE sets whether the export handler would overwrite an existing file
#the newly created exported file. If it is set to false the export handler would
#not overwrite. In this case if INTELLIGENTFILENAMING is set to true the handler
#would add a suffix to the new file name. The suffix is a randomly generated UUID.
#Additionally, you can add a timestamp or random number as additional prefix.

OVERWRITEFILE=false
INTELLIGENTFILENAMING=true
FILESUFFIXFORMAT=TIMESTAMP
 
If Ruby on Rails:
Open lib/fusioncharts/exporter/properties.rb and make changes in the following values there.

#IMPORTANT: You need to change the location of folder where
# the exported map images/PDFs will be saved on your
# server. Please specify the path to a folder with
# write permissions in the constant SAVE_PATH below.
# This path is relative to the web application root

@@SAVEPATH = "./images/"

#Used to show as message in SWF
#This constant HTTP_URI stores the HTTP reference to
#the folder where exported maps will be saved.
#Please enter the HTTP representation of that folder
#in this constant e.g., http://www.yourdomain.com/images/

@@HTTP_URI = "http://www.yourdomain.com/images/"

=begin
---------------------------- Export Settings -------------------------------
OVERWRITEFILE sets whether the export handler would overwrite an existing file
the newly created exported file. If it is set to false the export handler would
not overwrite. In this case if INTELLIGENTFILENAMING is set to true the handler
would add a suffix to the new file name. The suffix is a randomly generated UUID.
Additionally, you add a timestamp or random number as additional suffix.
=end

#Values allowed are true or false
@@OVERWRITEFILE = false
#Values allowed are true or false
@@INTELLIGENTFILENAMING = true
#Values allowed are "TIMESTAMP" or "RANDOM"
@@FILESUFFIXFORMAT = "TIMESTAMP"

 
Configuring the XML
As previously mentioned, we just need to do the following changes to XML:

<map borderColor='005879' fillColor='D7F4FF' numberSuffix=' Mill.' includeValueInLabels='0' labelSepChar=': ' baseFontSize='9' exportEnabled='1' exportAtClient='0' exportAction='save' exportHandler='http://www.domain.com/ExportHandlers/FCExporter.php' exportFileName='MyFileName'>
  <colorRange>
    <color minValue='0' maxValue='500' displayValue='Sparsely Populated' color='A7E9BC' />
    <color minValue='500' maxValue='1000' displayValue='Well Populated' color='FFFFCC' />
    <color minValue='1000' maxValue='5000' displayValue='Densely Populated' color='FF9377' />
  </colorRange>
  <data>
    <entity id='NA' value='515' />
    <entity id='SA' value='373' />
    <entity id='AS' value='3875' />
    <entity id='EU' value='727' />
    <entity id='AF' value='885' />
    <entity id='AU' value='32' />
  </data>
</map>

This instructs the map to save the exported image on server.

Note that we've not provided a file name here. If not provided, it takes the default value of "FusionCharts". You can, however, specify your file name using exportFileName attribute of <map> element. However, since in this case, the server path could already have another file name with the same name, the following conditional logic takes place:

  • If the folder path specified on server doesn't have a file name that matches this file, the image/PDF is created using this file name.
  • However, if another file with the same name exists, the over-writing is dependent on whether you've changed the over-write flag (as described above) in your server-side export handler.
    • If you've set it to yes, the export handler blindly over-writes the old file.
    • If you've set it no, the new name depends on whether you've opted for smart auto-naming.
      • If yes, the new name is decided by combining your specific file name + GUID + time stamp or random value (as chosen by value)
      • If auto-naming is set to off (when over-write is set to off as well), the export-handler throws an error, as it doesn't have any option left.
 
Setting up the call back handler

Now that everything else is setup, the only thing left is to define a JavaScript callback function handler. This callback function is invoked when the map gets a response back from server - in both success and failure cases.

To specify your callback function to map, you need to specify the function name as exportCallback attribute of <map> element as under:

<map ... exportCallback='myCallBackFunction' ...>

And you then need to define this function in your JavaScript code. However, if you do not define any call back function in your XML, the default callback function FC_Exported is invoked. In either case, an object is passed to the function as FC_Exported(objRtn) or myCallBackFunction(objRtn), which contains the following parameters (returned from server):

  • statusCode - Has the value of 1 in case of success, and 0 in case of failure.
  • statusMessage - In case of failure, this parameter contains a string description of the error (returned by server)
  • fileName - If saving was successful, this parameter contains the HTTP reference to the image/PDF file saved on server
  • width & height - If saving was successful, these parameters contain the width/height of saved image. Else, they contain 0.
  • DOMId - DOMId of the map that was successfully exported.

Let's quickly see an example code where a callback function has been implemented. In this example, once we receive the confirmation from server, we just show it in a JavaScript alert.

<html>
<script language="JavaScript" src="../../FusionMaps/FusionMaps.js"></script>
<script type="text/javascript">
   //Callback handler method which is invoked after the map has saved image on server.
   function FC_Exported(objRtn){
      if (objRtn.statusCode=="1"){
         alert("The map was successfully saved on server. The file can be accessed from " + objRtn.fileName);
      }else{
         alert("The map could not be saved on server. There was an error. Description : " + objRtn.statusMessage);
      }
   }
</script>
</head>

<body bgcolor="#ffffff">
   <div id="mapdiv" align="center">The map will appear within this DIV. This text will be replaced by the map.</div>
  <script type="text/javascript">
    //Create the map.
    var myMap = new FusionMaps("../../../Maps/FCMap_World.swf", "myMapId", "500", "266", "0", "1");
    myMap.setDataURL("SaveData.xml");
    myMap.render("mapdiv");
</script>
</body>
</html>

In the above code, note how the default FC_Exported method is called once the server-side export of map is completed.

When you run this code, you'll get the following output.

Here, since we've just one map in the page, we didn't put any if-then block to check the DOMId of the map. However, if you've multiple maps in a page, you can do use if-then statements (based on DOMId string match) to take a different course of action when each map in the page has been saved.