title: How to add filename and file extension to window.open method that exports a JSON array to a CSV string then to a file in javascript?
body: <p>I have the following code, which was found on the following question:
  <a href="https://stackoverflow.com/a/4130939/1055971">https://stackoverflow.com/a/4130939/1055971</a></p>
  
  <pre><code>function DownloadJSON2CSV(objArray)
  {
      var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
  
      var str = '';
  
      for (var i = 0; i &lt; array.length; i++) {
          var line = '';
  
          for (var index in array[i]) {
              line += array[i][index] + ',';
          }
  
          line.slice(0,line.Length-1); 
  
          str += line + '\r\n';
      }
  
      window.open( "data:text/csv;charset=utf-8," + escape(str))
  
  }
  </code></pre>
  
  <p>This code works very well.  It downloads the file and saves it, but the issue is there is not a way to add a custom title to the file, and also, the file saves with no extension.  <strong>For example, when the save function is called, the file name is 'download' by default with no file extension.</strong>  When i add an extension through the windows file system (like .csv) and open the file again, all data is perfectly preserved.<br>
  <strong>I am hoping to figure out a way to preset the file name and file type before the download takes place. Any ideas?</strong></p>