Skip to content
This repository has been archived by the owner on Nov 14, 2017. It is now read-only.

how to write file and auto download? #3

Open
thearabbit opened this issue Feb 10, 2015 · 14 comments
Open

how to write file and auto download? #3

thearabbit opened this issue Feb 10, 2015 · 14 comments

Comments

@thearabbit
Copy link

Excuse me, could you example for me.
how to write file and auto download?

@netanelgilad
Copy link
Owner

What is the scenario you are looking for? You have data in the server and you want the user to press a button in the client and have it download the file?
Do you need help with the download feature too or just writing the excel file to the server filesystem?

@thearabbit
Copy link
Author

I come from Laravel PHP framework, and i used PHPExcel package to generate reports with excel file template. So I want to generate report with excel template file and pass the data in to the template and download it (when i press a button on the form).

@guysmoilov
Copy link

I also have the same need.
I want the server to generate a dynamic excel file based on current data, and return it to the user immediately as a download, without storing a file on the server.

This django package has this exact functionality and works great : https://pypi.python.org/pypi/django-excel-response

@netanelgilad
Copy link
Owner

Finally got to it and wrote an example. Check it out here: http://meteorpad.com/pad/2hjNqmwHjDvkxvLC5/Leaderboard.
Tell me if you found it useful.

@thearabbit
Copy link
Author

Very thanks, I will try.

@dpatte
Copy link

dpatte commented Apr 7, 2015

I tried include the relevent code in my meteor app. I get a console.log message of
/gridfs/temporaryFiles/250966da1b279ad2cb590881
But it doesn't download the file, nor do I see the file anywhere on my server.
Any suggestions?

More details:
After clicking the button to execute the method it reports the path for the file, and I can manually download by visiting the console log path, such as:
http://mydomain/gridfs/temporaryFiles/250966da1b279ad2cb590881
but the link.click() in the event handler doesn't download the file itself.

More details:
I have discovered that it works using chrome (I am using v41), but doesn't work from firefox (my version is 37.0.1). Perhaps it is a difference in how firefox handles click() on a download link.

Perhaps the a element has to be attached to the dom and made visible for click to work?

@Waiski
Copy link

Waiski commented Apr 9, 2015

As @dpatte pointed out, it seems that the example does not work in Firefox 37.0.1. Adding: document.body.appendChild(link); before link.click() seems to fix this though.

I wonder however if it would be possible to implement js-xlsx's XLSX.write() -method in this package so that the written document could be just returned from a meteor method and then served with FileSaver.js in the front-end, like in the latter example in js-xlsx readme. This would result in significantly simpler code and remove the requirement for any temporary file collection.

@netanelgilad
Copy link
Owner

@Waiski I'm currently thinking about implementing a simpler solution. Using FileSaver.js is a possibility but the problem is that browser support doesn't seem optimal in the older browsers.
Downloading a file from a server is a feature that works in all browsers (sure the code that initiated the download needs to be cross-browser too, as the issue you mentioned..).

@dpatte
Copy link

dpatte commented May 17, 2015

I now have modified my code and have perhaps have a hint of the issue

Template.pipeline.events(
{
'click #excel': function()
{
Meteor.call('downloadExcelFile', function(err, fileUrl)
{
if (err)
alert(err);
else
{
var link = document.createElement("a");
link.href = fileUrl+'?download=true';
//link.target = '_blank';
var chLink = document.body.appendChild(link);
chLink.click();
document.body.removeChild(chLink);
alert('removed link xls');
}
});
},

Using the above, the eexcel file is generated and does download. everything looks fine until i OK the alert at which point all my subscriptions are gone. I have to do a page refresh to get the page to redraw correctly,
But if uncomment the link.target my page looks fine.
perhaps the document.append or remove are at fault?

@netanelgilad
Copy link
Owner

@dpatte If everything works fine without the link.target then maybe it is not needed and can be just removed?

@dpatte
Copy link

dpatte commented May 17, 2015

you missed my point, I believe.
Without enabling the link.target, the download causes the screen to lose its collections some how and I have to manually do a screen refresh.
I have to enable the link target workaround in order to download, which has its own issues - it is seen as a popup request by most browsers.

@dpatte
Copy link

dpatte commented May 17, 2015

I have raised my download issue with vsivsi of meteor-file-collection. I believe now the download issue is not a problem of meteor-excel but some issue with vsivsi 's file collection package.

@dpatte
Copy link

dpatte commented Sep 8, 2015

I have more details on this....

On Chrome, to automaticaly download I am using the code from the meteorpad example:
var link = document.createElement("a");
link.download = 'Players leaderboard.xlsx';
link.href = fileUrl;
link.click();
This works fine.
But for firefox, this does not work unless I add the following line before the link.click().
link=document.body.addChild(link);

In FF this now downloads, but causes another problem...the socket is lost, and the suscriptions are lost. Refreshing the page restores them.

@bestwestern
Copy link

The meteorpad is currently not loading.
I would also very much like to see an example and if possible one without Iron router (or any other router if that is possible) Could you perhaps put it on Github?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants