473,385 Members | 1,542 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,385 software developers and data experts.

Open a new window with POST method to download a file

dreamfalcon
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
5 11374
pronerd
392 Expert 256MB
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
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
pronerd
392 Expert 256MB
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
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
pronerd
392 Expert 256MB
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

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

Similar topics

12
by: Matt | last post by:
I want to write a ASP page to open/download a file In fileview.jsp, I have the file browse, and when user click submit button, fileview2.asp should open the file. fileview.asp ============...
3
by: Scott | last post by:
Here is the scenario. I need to launch a popup using window.open, but can't pass everything i need to pass on the querystring. So I thought I would set a cookie. Basically I do: ...
2
by: kevinm3574 | last post by:
So, I'm doing the following in a php script so that I can fire a download dialogue AND redirect the page (after clicking submit in a form). I'm doing it this way because of the problem with...
7
by: theyas | last post by:
How can I get my code to NOT display two "Open/Save/Cancel/More Info" dialog boxes when using the "Response.WriteFile" method to download a file to IE I've asked about this before and didn't get a...
0
by: Burt | last post by:
I have an ASP.NET page that performs a window.open to an HttpHandler that streams a binary file to the brower. I am forcing the save_as... dialog in IE. My issue is that in IE6 the new window...
3
by: | last post by:
Hello, I try to open a new Window in code behind with : ClientScript.RegisterClientScriptBlock(this.GetType(), "MyOpenScript", "window.open('toto.doc');", true); My problem is that the new...
6
by: mistral | last post by:
what is correct way open a PDF document in new window use hyperlink? I want show images thumbnails linked with PDF files, when click on thumbnail, PDF will be opened in new window. Some of PDF...
11
by: viki1967 | last post by:
Hi all. I have this form: <form method="POST" action="default.asp?ID=<%=strID%>"> <input type="image" src="download.gif"> </form> I need before the download file open window popup...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.