node-webkit file download problem

14 views
Skip to first unread message

tchi...@gmail.com

unread,
Jan 11, 2013, 11:34:58 PM1/11/13
to node-...@googlegroups.com
Hi,

Node-webkit only seems to be able download static files. When I click on a link to download a static file, the "Save As" dialog appears.

But when my web server dynamically builds the file the "Save As" dialog doesn't appear. In fact, nothing happens with node-webkit. In the browser, the file downloads without any trouble.

Here's an example of the code I use on my web server (java):

String clientDataXML = ... 

InputStream is = new ByteArrayInputStream(clientDataXML.getBytes());
IOUtils.copy(is, response.getOutputStream());
   
response.addHeader("Pragma", "no-cache");
response.addHeader("Expires", "0");
response.addHeader("Cache-Control", "no-cache,must-revalidate");
response.addHeader("Content-Type", "application/force-download");
response.addHeader("Content-Disposition", "attachment;filename=clientdata.xml");
response.addHeader("Content-Transfer-Encoding","binary");
response.flushBuffer();

Any ideas ? Do I need to setup the headers in a special way to make node-webkit "Save As" dialog appear ?

Thanks
Willy.

tchi...@gmail.com

unread,
Jan 11, 2013, 11:52:15 PM1/11/13
to node-...@googlegroups.com, tchi...@gmail.com

I removed all the header junk but only kept 

response.addHeader("Content-Disposition", "attachment;filename=clientdata.xml");

Works in the browsers but still not luck with node-webkit.

tchi...@gmail.com

unread,
Jan 12, 2013, 4:42:48 PM1/12/13
to node-...@googlegroups.com, tchi...@gmail.com
Ok, I managed to make it work.

Here's my solution:

Please understand that my app is actually a web application that I'm trying to port to node-webkit in order to force my users to use a single DOM engine so that I don't have to code against 3-4 browsers.

In the browser, I used the technique to change a hidden iframe's src attribute to trigger the download. With node-webkit this technique doesn't work anymore.

However, the 2nd technique works like a charm, I simply use window.location.href to trigger the download. Webkit realizes that the new location is not a web page and that it's actually a file. So, the "Save As" dialog now pops up to let the user choose a location to save the file.

For a while, I was dreading to have to go through a big loop using node.js API (http.get or http.request) to get the download feature going which I wasn't successful at because http.get or http.request isn't part of webkit. So, it wasn't sending session information to my web server and my web server processed the request as if the user needed to login before hand. It's as if http.get or http.request created an entirely new client (new client = new context) to communicate with the web server and isn't aware of the current context.

Sending session information with http.get or http.request has remained a mystery. The need to forward the session from node-webkit to an external entity such a web server may rise again but for the moment, I got what I needed working.

Thank god.

hallelujah.

Merry Xmas.

Happy New Year.

Be great.

Have fun.

Thank you!
Willy.

chaobin.zhang

unread,
Jan 13, 2013, 1:20:52 AM1/13/13
to node-...@googlegroups.com, tchi...@gmail.com
Great! Thanks for your sharing! 
I want to help when I see you post, but find I have little knowledge about this.
Reply all
Reply to author
Forward
0 new messages