public inbox for [email protected]  
help / color / mirror / Atom feed
From: Murtuza Zabuawala <[email protected]>
To: pgadmin-hackers <[email protected]>
Subject: PATCH: Fix the issue for saving query output as CSV
Date: Mon, 27 Jun 2016 17:38:30 +0530
Message-ID: <CAKKotZSvCZ4JkK03rgGyG5h8QAeoQrcDfxsguj81YX4EgWAZbA@mail.gmail.com> (raw)
List-Unsubscribe:  <mailto:[email protected]?body=unsub%20pgadmin-hackers>

Hi,

PFA patch to fix the issue for downloading query output as CSV not working
in Firefox & IE.
Also fixes the issue that HTML tags were also appended in columns title in
CSV header.

Browser side fix for -> RM#1405

--
Regards,
Murtuza Zabuawala
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


-- 
Sent via pgadmin-hackers mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgadmin-hackers


Attachments:

  [application/octet-stream] RM_1405.patch (3.0K, 3-RM_1405.patch)
  download | inline diff:
diff --git a/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js b/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js
index c2282a1..b0ca541 100644
--- a/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js
+++ b/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js
@@ -2571,7 +2571,7 @@ define(
                 keys = _.pluck(self.columns, 'name');
 
             // Fetch the items from fullCollection and convert it as csv format
-            var csv = labels.join(',') + '\n';
+            var csv = keys.join(',') + '\n';
             csv += coll.map(function(item) {
                 return _.map(keys, function(key) {
                   var cell = csv_col [key].cell,
@@ -2583,11 +2583,6 @@ define(
                 }).join(',');
             }).join('\n');
 
-            // Download the file.
-            var encodedUri = encodeURI('data:text/csv;charset=utf-8,' + csv),
-                    link = document.createElement('a');
-            link.setAttribute('href', encodedUri);
-
             /* If download is from view data then file name should be
              * the object name for which data is to be displayed.
              */
@@ -2598,8 +2593,7 @@ define(
                 success: function(res) {
                   if (res.data.status) {
                     filename = res.data.result + '.csv';
-                    link.setAttribute('download', filename);
-                    link.click();
+                    self._save_csv_file(csv, filename);
                   }
                 },
                 error: function(e) {
@@ -2622,14 +2616,32 @@ define(
             else {
               var cur_time = new Date();
               var filename = 'data-' + cur_time.getTime() + '.csv';
-              link.setAttribute('download', filename);
-              link.click();
+              self._save_csv_file(csv, filename);
             }
           }
           else {
             alertify.alert('Download Data', 'No data is available to download');
           }
         },
+        // We will use this function to save the file across browser
+        _save_csv_file: function(csvData, filename) {
+            var blob = new Blob([csvData], { type: 'text/csv;charset=utf-8;' });
+            if (navigator.msSaveBlob) { // IE 10+
+                navigator.msSaveBlob(blob, filename);
+            } else {
+                var link = document.createElement("a");
+                if (link.download !== undefined) { // feature detection
+                    // Browsers that support HTML5 download attribute
+                    var url = URL.createObjectURL(blob);
+                    link.setAttribute("href", url);
+                    link.setAttribute("download", filename);
+                    link.style.visibility = 'hidden';
+                    document.body.appendChild(link);
+                    link.click();
+                    document.body.removeChild(link);
+                }
+            }
+        },
 
         _auto_rollback: function() {
           var self = this;


view thread (23+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected]
  Subject: Re: PATCH: Fix the issue for saving query output as CSV
  In-Reply-To: <CAKKotZSvCZ4JkK03rgGyG5h8QAeoQrcDfxsguj81YX4EgWAZbA@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox