By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,046 Members | 1,350 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,046 IT Pros & Developers. It's quick & easy.

How to force browser download dialog

P: 2
I need to downlad a server side generated csv file with Javascript.
I need to send the login name and password and I don't want to use GET as I want to avoid a visible password in the URL.
Further, my displayed HTML page should not reload.

This is what I have tried:

1) Ajax:
Expand|Select|Wrap|Line Numbers
  1. var link = "http://.....";
  2. var base = base64Encode("k320i:xxxxx");
  3. var xhr = new XMLHttpRequest();
  4.   var async = false;
  5."POST", link, async);
  6.   xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  7.   xhr.setRequestHeader("Authorization", "Basic " + base);
  8.   xhr.send("a=XXXX");
-> This works fine, but no download dialog pops up in the browser, the response arrives as xhr.responseText

2) A hidden POST Form
Expand|Select|Wrap|Line Numbers
  1. <form id="downloadFormPOST" action="http://localhost:8080/io/rest/stat/k320i/2008-10-19/2009-10-20T12:00/test.csv" method="post" target="_blank">
  2.   <input type="hidden" name="_loginName" value="k320i">
  3.   <input type="hidden" name="_password" value="xxxx">
  4. </form>
And trigger it like this:
Expand|Select|Wrap|Line Numbers
  1. var downloadForm = window.document.forms['downloadFormPOST'];
  2.     downloadForm.submit();
But now the password is not transmitted
Can I somehow attach Basic authentication to above form?

Question: What is the correct approach?

Nov 17 '09 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 15k+
P: 16,027
What's the header set to for the CSV page? If you make a normal GET request, does it work?
Nov 18 '09 #2

P: 2
Hi again,

the "2) A hidden POST Form" works!
It was my mistake on server side (a Java REST servlet) during parsing the form data.

But "1) Ajax" I didn't find a way to popup the browser download dialog during a Ajax response inside the browser (via Javascript)

So I'm using now solution 2).

I'm still curious: How to attach HTTP basic authentication to the <form> markup?

Nov 18 '09 #3

Expert Mod 15k+
P: 16,027 - a bit experimental, but should help.
Nov 19 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.