473,322 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How to force browser download dialog

2
Hi,
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.   xhr.open("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?


Thanks
Marcel
Nov 17 '09 #1
3 5005
acoder
16,027 Expert Mod 8TB
What's the header set to for the CSV page? If you make a normal GET request, does it work?
Nov 18 '09 #2
swand9
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?

thanks
Marcel
Nov 18 '09 #3
acoder
16,027 Expert Mod 8TB
http://www.peej.co.uk/articles/http-...tml-forms.html - a bit experimental, but should help.
Nov 19 '09 #4

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

Similar topics

1
by: Navin | last post by:
hi, guys i am using the following code to force a file download dialog in asp Response.ContentType = "application/vnd.ms-excel" response.AddHeader "content-disposition","attachment; filename="...
1
by: Navin | last post by:
hi, guys i am using the following code to force a file download dialog in asp Response.ContentType = "application/vnd.ms-excel" response.AddHeader "content-disposition","attachment; filename="...
4
by: Kevin Muenzler, WB5RUE | last post by:
How do I force a browser to download a file instead of displaying it? In other words I have a page with MP3 and WMA files on it and I would like for the visitor to download the file instead of...
0
by: PJS | last post by:
Thanks in advance. I have a site which generates a user specific XML document. The user then selects "Save to PC" which then forces the browser to show the "save/download" dialog box. The code...
2
by: Sam-Kiwi | last post by:
I've spent the last 6 months developing a pay-per-download website using ASP.NET Users purchase documents and then download them. The intention is that users are only charged for documents...
1
by: greg | last post by:
Hi I have a link on a page that points to a file inside the site that I want to be downloaded loke in page_load I write linkFile.NavigateUrl = Request.ApplicationPath + "/somepath/file.iif";...
4
by: Richard Wilde | last post by:
How do I force an XML document to be downloaded from a web browser to a client? The XML document will come ideally from a dataset but if this is not possible then I can save off the XML document...
0
by: jinnareddy | last post by:
Hi, I'm unable to download a file that is having a 2-byte char in its name (e.g.テ) using force download option. Though, am able to download file names involving ASCII chars. I have tried URL...
1
by: JP SIngh | last post by:
Hi All We have a page which we want to allow our users to download files. Can someone point to peice of code that we can use to force the download as opposed to opening the files in the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.