0

In my Webix application, I am trying to download a file with a specific filename either taken from user or contstructed programmatically. The download functionality is working fine, however the file is always getting downloaded with the name 'download'.

Browser is Chrome. And I need to make it work on both Chrome and Firefox. Below is my working code for download:

Webix part :

mytoolbar =
  { id:'toolbarID',
    view: 'toolbar',
    height: rowHeight,
    type: 'clean',
    cols: [
      {view:"button", id:"downld", label:"Download", width:100, tooltip: "Download data", on: {onItemClick:function(){downloadData()}} },
      ]
   }

The callback function :

function downloadData() {
    var $$ref = $$('mytext');
    var value = $$ref.getValue();
    var msg = "This is the " + value + "of the port";
    // assume that the filename will be also be constructed like msg as 
    // as above.

    document.location = 'data:Application/octet-stream,' + encodeURIComponent(msg)

}

I have vainly tried below things:

Webix download function which I need to download as .txt file, not working :

var $$ref = $$('mytext');
var value = $$ref.getValue();
var msg = "This is the " + value + "of the port";
var blob = new Blob([msg.toString()], { type: "application/pdf" });
webix.html.download(blob, "myfile.pdf");

Below one is working but, I am not able to pass a variable for the text and the filename. It is working only for hard coded text and filename. Any hint to provide the variable would be helpful. :

function downloadData() {
$('<a href="data:text/plain,Test" download="test.txt">')[0].click()
}

Below attempt is partially working. It is always downloading the file with a long text like "235d67c8-892f-465d-95c4-17bc3cc51849". Assume variable msg is as same as above :

function downloadData() {
 var msg = "Contstucted as above by string concatenation";
 var filename = file + ".txt"; //assume file is a var
 var blob = new File([msg], filename, {"type": "application/octet-stream"});

  var a = document.createElement("a");
  a.href = URL.createObjectURL(blob);

  window.location.href=a;

}

If I change the Webix button to anchor ( ) as below and pass download= "filename", that is also not working, however in this case there is no way to dynamically pass the filename variable.

{ template:"<a id = \"link\", onclick=\"downloadData()\" ,  href = \"#\" >Download</a>" },

Is there a way to achieve this when the front end is Webix ? The examples have been tried from various links on stackoverflow only, some are given below:

Is there any way to specify a suggested filename when using data: URI?
How to set a downloading file name using window.open in javascript

Community
  • 1
  • 1

1 Answers1

1

Your code actually works for me ( or course the result file is not a PDF file, but it contains the specified name and content )

http://webix.com/snippet/400b9001

Such code will fail for Safari on Mac, though. Safari doesn't support setting a custom name for the file to be downloaded. The good news, this functionality is already added in the latest dev. preview of Safari, so functionality will work correctly win the next Mac OS update.

Aquatic
  • 5,084
  • 3
  • 24
  • 28
  • thanks for your reply and confirming that it is working. However, when I am doing an inspect element when it is not downloading, both FF & chrome browser console says "Uncaught TypeError: webix.html.download is not a function" . Even in the chrome console if I type "webix.html." the function list is not showing any 'download' function in the avaialble list. But webix.message() and other such webix functions are working. Do I need to include any library for this to work ? – A.G.Progm.Enthusiast Apr 27 '17 at 16:03
  • I updated my webix version and it has started working. Thank you! – A.G.Progm.Enthusiast Apr 27 '17 at 16:56