javascript document.location to download a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ciary
    Recognized Expert New Member
    • Apr 2009
    • 247

    javascript document.location to download a file

    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.locati on.

    here is my ajax request and the code to redirect my page.
    Code:
    			function Exportfile(){
                                    var data = "--- content of 'a lot of data' goes here ---";
    				
    				XMLHttpRequestObject = GetXmlHttpObject();
    				var sended = "data="+data;
    				var download = "download.php";
    				XMLHttpRequestObject.open("POST",download,true);
    				
    				XMLHttpRequestObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    				XMLHttpRequestObject.setRequestHeader("Content-length", sended.length);
    				XMLHttpRequestObject.setRequestHeader("Connection", "close");
    				
    				XMLHttpRequestObject.onreadystatechange = function(){
    					if (XMLHttpRequestObject.readyState == 4){
    						filename = XMLHttpRequestObject.responseText;
    						document.location = filename;
    					}
    				}
    				
    				XMLHttpRequestObject.send(sended);
    			}
    does anyone know how i can do this?
  • Ciary
    Recognized Expert New Member
    • Apr 2009
    • 247

    #2
    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

    Comment

    Working...