I have a link when clicked processes a coldfusion page that downloads a file from the server. When I access the file directly in the url, it works as it should, so I know that part is working. My next step is to introduce ajax so the interaction is seamless to the user. This is my event handler with the ajax call (event handler is called on initial page load):
function fileHandler(){
$('#attachment').click(function(e){
e.preventDefault();
$.ajax({
type:"get",
url:"test.cfm",
data:{name:"john"}
});
});
}
But I get nothing. This is my first time using jQuery’s .ajax(). I normally use .get() and .load(). I’ve checked the documentation and my syntax appears correct. Does anyone see the problem of why I can’t get the browser prompt to save the file? Thanks!
You cannot trigger a “Save As” dialog trough Ajax (XMLHTTPRequest). An Ajax call returns data from your server to your javascript application, that’s the whole point about using Ajax. Downloading a file and saving it on the computer is just another use-case – that is a “browsers job”, not yours.
I resolved that “issue” by appending a hidden iframe to the page, this will trigger a “Save as…” dialog and the user won’t notice it. It’s like opening an address in another tab, just that it’s hidden.