471,893 Members | 1,363 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

javascript document.location to download a file

247 Expert 100+
hey all,

i have another problem. basically the question is: how do i make sure a file is downloaded instead of opened?

but i'll explain the situation. i'm trying to download a file using a button. when clicked, i call an ajax-request to a php-page where i dynamically create a ".pmp"-file. then it returns the name of the file. last, i open the file using document.location.

here is my ajax request and the code to redirect my page.
Expand|Select|Wrap|Line Numbers
  1.             function Exportfile(){
  2.                                 var data = "--- content of 'a lot of data' goes here ---";
  4.                 XMLHttpRequestObject = GetXmlHttpObject();
  5.                 var sended = "data="+data;
  6.                 var download = "download.php";
  7.                 XMLHttpRequestObject.open("POST",download,true);
  9.                 XMLHttpRequestObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  10.                 XMLHttpRequestObject.setRequestHeader("Content-length", sended.length);
  11.                 XMLHttpRequestObject.setRequestHeader("Connection", "close");
  13.                 XMLHttpRequestObject.onreadystatechange = function(){
  14.                     if (XMLHttpRequestObject.readyState == 4){
  15.                         filename = XMLHttpRequestObject.responseText;
  16.                         document.location = filename;
  17.                     }
  18.                 }
  20.                 XMLHttpRequestObject.send(sended);
  21.             }
does anyone know how i can do this?
May 8 '09 #1
1 12651
247 Expert 100+
never mind. i found a solution on the last page of my search on the forum. i was attracted to the number of views.

anyway, here is what you need to do
May 8 '09 #2

Post your reply

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

Similar topics

5 posts views Thread by John Taylor-Johnston | last post: by
2 posts views Thread by Graham | last post: by
reply views Thread by cephelo | last post: by
reply views Thread by YellowAndGreen | last post: by

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.