<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>FreeMASTER中的主题 Re: FreeMASTER Lite data export to .csv format</title>
    <link>https://community.nxp.com/t5/FreeMASTER/FreeMASTER-Lite-data-export-to-csv-format/m-p/1643583#M1328</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.nxp.com/t5/user/viewprofilepage/user-id/194842"&gt;@prashanth_dhundra&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;Unfortunately FreeMASTER Lite has no native support to export data directly into CSV. But there are 2 way to achieve this:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Using File API exposed by FreeMASTER Lite&lt;/LI&gt;&lt;LI&gt;Using Web Browser capabilities via JS&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;1. F. Lite API exposes functions allowing reading from and writing to files. First you need to specify what file permissions in your project file ex:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;{
  "port": 8090,
  "web_root": "C:/NXP/FreeMASTER 3.2/FreeMASTER Lite/html",
  "dirs": [
    {
      "path": "C:/NXP/FreeMASTER 3.2/FreeMASTER Lite",
      "opts": "w+",
      "exts": ".csv"
    }
  ]
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note: by default all File IO operations are restricted the. In order to work with files, users need to specify explicitly the &lt;EM&gt;&lt;STRONG&gt;path&lt;/STRONG&gt;&lt;/EM&gt; (root folder), &lt;EM&gt;&lt;STRONG&gt;exts&lt;/STRONG&gt;&lt;/EM&gt; (list of allowed file extensions) and &lt;EM&gt;&lt;STRONG&gt;opts &lt;/STRONG&gt;&lt;/EM&gt;(list of allowed modes/flags).&lt;/P&gt;&lt;P&gt;And here's how you would use the API in your application:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;&amp;lt;html&amp;gt;  
&amp;lt;head&amp;gt;  
   &amp;lt;title&amp;gt; Download CSV file using FMSTR File API &amp;lt;/title&amp;gt;
  &amp;lt;script type="text/javascript" src="./simple-jsonrpc-js.js"&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;script type="text/javascript" src="./freemaster-client.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/head&amp;gt;  
&amp;lt;body onload="InitPCM()"&amp;gt;  
  &amp;lt;h3&amp;gt; Click the button to save data to CSV file &amp;lt;/h3&amp;gt;  
    
  &amp;lt;!-- create an HTML button to save data the CSV file on click --&amp;gt;  
  &amp;lt;button onclick="save_csv_file()"&amp;gt; Save CSV &amp;lt;/button&amp;gt;  

  &amp;lt;script&amp;gt; 
    var pcm;

    //create CSV file data in an array  
    var csvFileData = [  
      ['Col 1 Val 1', 'Col 2 Val 1'],  
      ['Col 1 Val 2', 'Col 2 Val 2'],  
      ['Col 1 Val 3', 'Col 2 Val 3'],  
      ['Col 1 Val 4', 'Col 2 Val 4'],  
      ['Col 1 Val 5', 'Col 2 Val 5']  
    ];  
        
    //create a user-defined function to save the CSV file   
    async function save_csv_file() {  
    
      //define the heading for each row of the data  
      var csv = 'Column 1,Column 2\n';  
        
      //merge the data with CSV  
      csvFileData.forEach(function(row) {  
        csv += row.join(',');  
        csv += "\n";  
      });  
    
      try {
        let response = await pcm.LocalFileOpen("C:/NXP/FreeMASTER 3.2/FreeMASTER Lite/data.csv", 'w+');
        let handle = response.data;
        await pcm.LocalFileWriteString(handle, csv);
        await pcm.LocalFileClose(handle);
        console.log("File saved");
      } catch(err) {
        on_error(err);
      }
    }
    
    function InitPCM() { 
      pcm = new PCM("localhost:8090", console.log, on_error, on_error);
      pcm.OnServerError = on_error;
      pcm.OnSocketError = on_error;
    }

    // Notify user about an error
    function on_error(err) {
      console.error(err);
    }
  &amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;  
&amp;lt;/html&amp;gt;  &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note the &lt;EM&gt;&lt;STRONG&gt;save_csv_file&lt;/STRONG&gt;&lt;/EM&gt; function:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;First, you need to create an in-memory object that will hold your CSV data.&lt;/LI&gt;&lt;LI&gt;Then, you convert it into a string.&lt;/LI&gt;&lt;LI&gt;Finally, you use the API to write data to the file in a C-like approach.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;This simple page contains one single button - when pressed it will write data to disk (debug logs a printed in the browser console).&lt;/P&gt;&lt;P&gt;2. Second approach is a web oriented&lt;/P&gt;&lt;P&gt;In this case no changes are required on F. Lite side because all processing is done on client side (browser):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;html&amp;gt;  
&amp;lt;head&amp;gt;  
  &amp;lt;title&amp;gt; Download CSV file via HTML/JS &amp;lt;/title&amp;gt;  
&amp;lt;/head&amp;gt;  
&amp;lt;body&amp;gt;  
  &amp;lt;h3&amp;gt; Click the button to download the CSV file &amp;lt;/h3&amp;gt;  
    
  &amp;lt;!-- create an HTML button to download the CSV file on click --&amp;gt;  
  &amp;lt;button onclick="download_csv_file()"&amp;gt; Download CSV &amp;lt;/button&amp;gt;  


  &amp;lt;script&amp;gt;  
    //create CSV file data in an array  
    var csvFileData = [  
        ['Col 1 Val 1', 'Col 2 Val 1'],  
        ['Col 1 Val 2', 'Col 2 Val 2'],  
        ['Col 1 Val 3', 'Col 2 Val 3'],  
        ['Col 1 Val 4', 'Col 2 Val 4'],  
        ['Col 1 Val 5', 'Col 2 Val 5']  
    ];  
            
    //create a user-defined function to download CSV file   
    function download_csv_file() {  
    
        //define the heading for each row of the data  
        var csv = 'Column 1,Column 2\n';  
            
        //merge the data with CSV  
        csvFileData.forEach(function(row) {  
        csv += row.join(',');  
        csv += "\n";  
        });  
            
        var hiddenElement = document.createElement('a');  
        hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv);  
        hiddenElement.target = '_blank';  
            
        //provide the name for the CSV file to be downloaded  
        hiddenElement.download = 'data.csv';  
        hiddenElement.click();
    }
  &amp;lt;/script&amp;gt;
 &amp;lt;/body&amp;gt;  
&amp;lt;/html&amp;gt;  &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;CSV content creation is quite similar to the previous approach. The difference - is the way the file is saved: the browser has no access to the file system, thus the CSV content is encoded into a URL and added to hidden hyperlink. When the download button is pressed - the browser prompts a download dialog asking the user to select the location where the file is saved.&lt;/P&gt;&lt;P&gt;Hope it helps,&lt;BR /&gt;Iulian&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 03 May 2023 11:31:29 GMT</pubDate>
    <dc:creator>iulian_stan</dc:creator>
    <dc:date>2023-05-03T11:31:29Z</dc:date>
    <item>
      <title>FreeMASTER Lite data export to .csv format</title>
      <link>https://community.nxp.com/t5/FreeMASTER/FreeMASTER-Lite-data-export-to-csv-format/m-p/1643333#M1327</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;Is there any way to generate a .csv file with FreeMASTER Lite?&lt;/P&gt;
&lt;P&gt;Please share any relevant information.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;Prashanth.&lt;/P&gt;</description>
      <pubDate>Wed, 03 May 2023 01:01:13 GMT</pubDate>
      <guid>https://community.nxp.com/t5/FreeMASTER/FreeMASTER-Lite-data-export-to-csv-format/m-p/1643333#M1327</guid>
      <dc:creator>prashanth_dhundra</dc:creator>
      <dc:date>2023-05-03T01:01:13Z</dc:date>
    </item>
    <item>
      <title>Re: FreeMASTER Lite data export to .csv format</title>
      <link>https://community.nxp.com/t5/FreeMASTER/FreeMASTER-Lite-data-export-to-csv-format/m-p/1643583#M1328</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.nxp.com/t5/user/viewprofilepage/user-id/194842"&gt;@prashanth_dhundra&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;Unfortunately FreeMASTER Lite has no native support to export data directly into CSV. But there are 2 way to achieve this:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Using File API exposed by FreeMASTER Lite&lt;/LI&gt;&lt;LI&gt;Using Web Browser capabilities via JS&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;1. F. Lite API exposes functions allowing reading from and writing to files. First you need to specify what file permissions in your project file ex:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;{
  "port": 8090,
  "web_root": "C:/NXP/FreeMASTER 3.2/FreeMASTER Lite/html",
  "dirs": [
    {
      "path": "C:/NXP/FreeMASTER 3.2/FreeMASTER Lite",
      "opts": "w+",
      "exts": ".csv"
    }
  ]
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note: by default all File IO operations are restricted the. In order to work with files, users need to specify explicitly the &lt;EM&gt;&lt;STRONG&gt;path&lt;/STRONG&gt;&lt;/EM&gt; (root folder), &lt;EM&gt;&lt;STRONG&gt;exts&lt;/STRONG&gt;&lt;/EM&gt; (list of allowed file extensions) and &lt;EM&gt;&lt;STRONG&gt;opts &lt;/STRONG&gt;&lt;/EM&gt;(list of allowed modes/flags).&lt;/P&gt;&lt;P&gt;And here's how you would use the API in your application:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;&amp;lt;html&amp;gt;  
&amp;lt;head&amp;gt;  
   &amp;lt;title&amp;gt; Download CSV file using FMSTR File API &amp;lt;/title&amp;gt;
  &amp;lt;script type="text/javascript" src="./simple-jsonrpc-js.js"&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;script type="text/javascript" src="./freemaster-client.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/head&amp;gt;  
&amp;lt;body onload="InitPCM()"&amp;gt;  
  &amp;lt;h3&amp;gt; Click the button to save data to CSV file &amp;lt;/h3&amp;gt;  
    
  &amp;lt;!-- create an HTML button to save data the CSV file on click --&amp;gt;  
  &amp;lt;button onclick="save_csv_file()"&amp;gt; Save CSV &amp;lt;/button&amp;gt;  

  &amp;lt;script&amp;gt; 
    var pcm;

    //create CSV file data in an array  
    var csvFileData = [  
      ['Col 1 Val 1', 'Col 2 Val 1'],  
      ['Col 1 Val 2', 'Col 2 Val 2'],  
      ['Col 1 Val 3', 'Col 2 Val 3'],  
      ['Col 1 Val 4', 'Col 2 Val 4'],  
      ['Col 1 Val 5', 'Col 2 Val 5']  
    ];  
        
    //create a user-defined function to save the CSV file   
    async function save_csv_file() {  
    
      //define the heading for each row of the data  
      var csv = 'Column 1,Column 2\n';  
        
      //merge the data with CSV  
      csvFileData.forEach(function(row) {  
        csv += row.join(',');  
        csv += "\n";  
      });  
    
      try {
        let response = await pcm.LocalFileOpen("C:/NXP/FreeMASTER 3.2/FreeMASTER Lite/data.csv", 'w+');
        let handle = response.data;
        await pcm.LocalFileWriteString(handle, csv);
        await pcm.LocalFileClose(handle);
        console.log("File saved");
      } catch(err) {
        on_error(err);
      }
    }
    
    function InitPCM() { 
      pcm = new PCM("localhost:8090", console.log, on_error, on_error);
      pcm.OnServerError = on_error;
      pcm.OnSocketError = on_error;
    }

    // Notify user about an error
    function on_error(err) {
      console.error(err);
    }
  &amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;  
&amp;lt;/html&amp;gt;  &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note the &lt;EM&gt;&lt;STRONG&gt;save_csv_file&lt;/STRONG&gt;&lt;/EM&gt; function:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;First, you need to create an in-memory object that will hold your CSV data.&lt;/LI&gt;&lt;LI&gt;Then, you convert it into a string.&lt;/LI&gt;&lt;LI&gt;Finally, you use the API to write data to the file in a C-like approach.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;This simple page contains one single button - when pressed it will write data to disk (debug logs a printed in the browser console).&lt;/P&gt;&lt;P&gt;2. Second approach is a web oriented&lt;/P&gt;&lt;P&gt;In this case no changes are required on F. Lite side because all processing is done on client side (browser):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;html&amp;gt;  
&amp;lt;head&amp;gt;  
  &amp;lt;title&amp;gt; Download CSV file via HTML/JS &amp;lt;/title&amp;gt;  
&amp;lt;/head&amp;gt;  
&amp;lt;body&amp;gt;  
  &amp;lt;h3&amp;gt; Click the button to download the CSV file &amp;lt;/h3&amp;gt;  
    
  &amp;lt;!-- create an HTML button to download the CSV file on click --&amp;gt;  
  &amp;lt;button onclick="download_csv_file()"&amp;gt; Download CSV &amp;lt;/button&amp;gt;  


  &amp;lt;script&amp;gt;  
    //create CSV file data in an array  
    var csvFileData = [  
        ['Col 1 Val 1', 'Col 2 Val 1'],  
        ['Col 1 Val 2', 'Col 2 Val 2'],  
        ['Col 1 Val 3', 'Col 2 Val 3'],  
        ['Col 1 Val 4', 'Col 2 Val 4'],  
        ['Col 1 Val 5', 'Col 2 Val 5']  
    ];  
            
    //create a user-defined function to download CSV file   
    function download_csv_file() {  
    
        //define the heading for each row of the data  
        var csv = 'Column 1,Column 2\n';  
            
        //merge the data with CSV  
        csvFileData.forEach(function(row) {  
        csv += row.join(',');  
        csv += "\n";  
        });  
            
        var hiddenElement = document.createElement('a');  
        hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv);  
        hiddenElement.target = '_blank';  
            
        //provide the name for the CSV file to be downloaded  
        hiddenElement.download = 'data.csv';  
        hiddenElement.click();
    }
  &amp;lt;/script&amp;gt;
 &amp;lt;/body&amp;gt;  
&amp;lt;/html&amp;gt;  &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;CSV content creation is quite similar to the previous approach. The difference - is the way the file is saved: the browser has no access to the file system, thus the CSV content is encoded into a URL and added to hidden hyperlink. When the download button is pressed - the browser prompts a download dialog asking the user to select the location where the file is saved.&lt;/P&gt;&lt;P&gt;Hope it helps,&lt;BR /&gt;Iulian&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 May 2023 11:31:29 GMT</pubDate>
      <guid>https://community.nxp.com/t5/FreeMASTER/FreeMASTER-Lite-data-export-to-csv-format/m-p/1643583#M1328</guid>
      <dc:creator>iulian_stan</dc:creator>
      <dc:date>2023-05-03T11:31:29Z</dc:date>
    </item>
  </channel>
</rss>

