469,097 Members | 1,556 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,097 developers. It's quick & easy.

javascript document.location to download a file

Ciary
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 ---";
  3.  
  4.                 XMLHttpRequestObject = GetXmlHttpObject();
  5.                 var sended = "data="+data;
  6.                 var download = "download.php";
  7.                 XMLHttpRequestObject.open("POST",download,true);
  8.  
  9.                 XMLHttpRequestObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  10.                 XMLHttpRequestObject.setRequestHeader("Content-length", sended.length);
  11.                 XMLHttpRequestObject.setRequestHeader("Connection", "close");
  12.  
  13.                 XMLHttpRequestObject.onreadystatechange = function(){
  14.                     if (XMLHttpRequestObject.readyState == 4){
  15.                         filename = XMLHttpRequestObject.responseText;
  16.                         document.location = filename;
  17.                     }
  18.                 }
  19.  
  20.                 XMLHttpRequestObject.send(sended);
  21.             }
does anyone know how i can do this?
May 8 '09 #1
1 12527
Ciary
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 zhoujie | last post: by
reply views Thread by kglaser89 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.