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

Open a new window with POST method to download a file

dreamfalcon
P: 20
Hi!
I have an AJAX function that sendís a string by the post method and display the result in the same page:
Expand|Select|Wrap|Line Numbers
  1.   xmlHttp.onreadystatechange = stateChanged;
  2.   xmlHttp.open('POST', url, true);
  3.   xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  4.   xmlHttp.setRequestHeader("Content-length", parameters.length);
  5.   xmlHttp.setRequestHeader("Connection", "close");
  6.   xmlHttp.send(parameters);
  7.   //.....
  8. document.getElementById(id).innerHTML=xmlHttp.responseText;
But now I want another function that sendís the same string to the server (PHP), and the result is an Excel file, that it should display the download dialog.

Any ideas?

Thx in advance
Oct 12 '07 #1
Share this Question
Share on Google+
5 Replies


Expert 100+
P: 392
But now I want another function that sendís the same string to the server (PHP), and the result is an Excel file, that it should display the download dialog.
I am not sure I follow. You want to be able to send the same URL string to a server, but be able to get back two different results? The server side logic has to be able to differentiate between the two requests. If both strings are the same it will not know when to send HTML to your current window, or when to send the Excel file to a new window.

If on the other hand all you want to do is pass a request back that will down load a file, it works the same way it would any other time. Just pass the URL. I dont think you want to use a popup window though. They are almost always blocked now.

It would be safer to use an iFrame. You just add the tags and when you are ready just set the src attribute to the URL you want to pass. The other nice thing about this is that you can set the iFrame to be 1px X 1px so the user does not even have to see it.

[HTML]<IFRAME id="downloadFile" ></IFRAME>

<script type="text/javascript">
document.getElementById('downloadFile').src='<<You r URL HERE>>';
</script>[/HTML]

If you want to force the save dialog box, so they can not automatically open the file, then just set the return MIME type to an invalided value. ("application/unknown")
Oct 12 '07 #2

dreamfalcon
P: 20
I am not sure I follow. You want to be able to send the same URL string to a server, but be able to get back two different results?
The server scripts are different. One for the tables that are displayed, and one for the excel file, both process the same string, but with different results.

<IFRAME id="downloadFile" ></IFRAME>
I tried:
Expand|Select|Wrap|Line Numbers
  1.  
  2. xmlHttpExcel.open('POST', url, true);
  3. //...
  4. document.getElementById('downloadFile').src=xmlHttpExcel.responseText;
  5.  
and in the excel.php:

Expand|Select|Wrap|Line Numbers
  1.  
  2. $hoje=date("Y_m_j");              
  3. header("Content-type: application/x-msdownload");
  4. header("Content-Disposition: attachment; filename=".$table."_".$hoje.".xls");
  5. header("Pragma: no-cache");
  6. header("Expires: 0");
  7. print "$header\n$data";  
  8.  
But still cant pass the string by post method for the php file :(
Oct 15 '07 #3

Expert 100+
P: 392
The server scripts are different. One for the tables that are displayed, and one for the excel file, both process the same string, but with different results.
So how does the server know which script to use if you are passing the exact same URL?



Expand|Select|Wrap|Line Numbers
  1.  
  2. xmlHttpExcel.open('POST', url, true);
  3. //...
  4. document.getElementById('downloadFile').src=xmlHttpExcel.responseText;
  5.  
First off, why are you using AJAX for this call. It does not look like their is any purpose for it. It looks like you already have the URL. All you need to do is to set the src attribute to be the url value you already have. Instead it looks like you are setting the src attributre to be the response header text.

If I am following what you want all you should need is

Expand|Select|Wrap|Line Numbers
  1.  
  2. <iframe id="downloadExcelFile" ></iframe>
  3.  
Expand|Select|Wrap|Line Numbers
  1.  
  2. document.getElementById('downloadFile').src=url;
  3.  




Expand|Select|Wrap|Line Numbers
  1.  
  2. $hoje=date("Y_m_j");              
  3. header("Content-type: application/x-msdownload");
  4. header("Content-Disposition: attachment; filename=".$table."_".$hoje.".xls");
  5. header("Pragma: no-cache");
  6. header("Expires: 0");
  7. print "$header\n$data";  
  8.  
I am not a PHP guy, but I do not see anything there that would generate an Excel file. That just looks like it is setting the MIME type for the HTTP response.


But still cant pass the string by post method for the php file :(
How do you know the URL is not being passed? As soon as you set the src attribute of the iFrame a request will be sent to the URL you passed to it.
Oct 15 '07 #4

dreamfalcon
P: 20
So how does the server know which script to use if you are passing the exact same URL?
the url is not the same, just the string that is passed by the POST method, I use diferent PHP scripts.

First off, why are you using AJAX for this call. It does not look like their is any purpose for it. It looks like you already have the URL.
I have the url of the script, but I don’t know how to pass the string for the script without using Ajax. The string is build using javascript.

Expand|Select|Wrap|Line Numbers
  1.  
  2. document.getElementById('downloadFile').src=url;
  3.  
Where I pass the string to the PHP script with the POST method? I can’t use GET

I am not a PHP guy, but I do not see anything there that would generate an Excel file. That just looks like it is setting the MIME type for the HTTP response.
I didn’t show all the code, just the final part. But it generate fine, this is not the problem.

Let me make the question again. How can I pass a string by the POST method using JavaScript to a PHP file and show a download file dialog without change the page?
Thx
Oct 16 '07 #5

Expert 100+
P: 392
I have the url of the script, but I donít know how to pass the string for the script without using Ajax. The string is build using javascript.
Ok, I think I see the confusion here. I think what you mean to say is that you want to pass a parameter, and your server side listener can only accept an HTTP POST Request.

So the easy way to do that would be to use the iFrame as I mentioned, and have it submit an HTML form element. That will pass the string you generate as an HTTP parameter in an HTTP POST REQUEST.


Expand|Select|Wrap|Line Numbers
  1. <FORM id="formSubmit" action="urlOfYourPHP.file" >
  2.     <INPUT id="StringToSubmit" name="paraterName" value="valueToPassHere"  >
  3. </FORM>
  4. <script type="text/javascript">
  5.     var formObj = document.getElementById("formSubmit");
  6.     formObj.submit();
  7. </script>
  8.  
  9.  
Oct 17 '07 #6

Post your reply

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