By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,699 Members | 1,318 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,699 IT Pros & Developers. It's quick & easy.

File Download with AJAX

P: 1
Hi,
I've a requirement which needs file download to be called from AJAX. can we make a call from AJAX so that content gets downloaded and file dialog box gets opened. Please help me.
Mar 2 '07 #1
Share this Question
Share on Google+
4 Replies


Expert 100+
P: 392
can we make a call from AJAX so that content gets downloaded and file dialog box gets opened. Please help me.
There might be some way, but it will be very hard and unreliable since what you are wanting to do is very unsafe and browsers do lots of things to try and stop this.

Best bet would be to make a button or link appear for the user to click on to start the download, or drop the AJAX requirement and just have them click on something. Then set the response MIME type to something like "forced download". That will guarantee they get the dialog instead of automatically trying to open the file.
Mar 4 '07 #2

mohsenhosseini
P: 28
Hi,
I've a requirement which needs file download to be called from AJAX. can we make a call from AJAX so that content gets downloaded and file dialog box gets opened. Please help me.
hi dear,
there is so simple way.
yes you can do this.
use an iframe with visibility:hidden,
then need to change the IFrame location to a downloadable file.

TESTED in IE,FF
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>Untitled Document</title>
  6. <script language="javascript">
  7.  
  8.     function tryToDownload(url)
  9.     {
  10.  
  11.         oIFrm = document.getElementById('myIFrm');
  12.         oIFrm.src = url;
  13.         alert(url);
  14.  
  15.     }
  16.     window.onload = setTimeout('tryToDownload("test.rar")', 5*1000);
  17.  
  18. </script>
  19. </head>
  20.  
  21. <body>
  22.  
  23. <h1>DontChange the Location</h1>
  24. <iframe id="myIFrm" src="" style="visibility:hidden">
  25. </iframe>
  26.  
  27. </body>
  28. </html>
  29.  
so easy ;)

best regards.
mh
Mar 4 '07 #3

mohsenhosseini
P: 28
Hi,
I've a requirement which needs file download to be called from AJAX. can we make a call from AJAX so that content gets downloaded and file dialog box gets opened. Please help me.

and to donwload the dynamic file contents

hi dear,
there is so simple way.
yes you can do this.
use an iframe with visibility:hidden,
then need to change the IFrame location to a downloadable file.

TESTED in IE,FF
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>Untitled Document</title>
  6. <script language="javascript">
  7.  
  8.     function tryToDownload(url)
  9.     {
  10.  
  11.         oIFrm = document.getElementById('myIFrm');
  12.         oIFrm.src = url;
  13.         alert(url);
  14.  
  15.     }
  16.     window.onload = setTimeout('tryToDownload("fileloader.php?fileName=test.rar")', 5*1000);
  17.  
  18. </script>
  19. </head>
  20.  
  21. <body>
  22.  
  23. <h1>DontChange the Location</h1>
  24. <iframe id="myIFrm" src="" style="visibility:hidden">
  25. </iframe>
  26.  
  27. </body>
  28. </html>
  29.  
and inside your server side code (like my php)
filename : fileloader.php

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. // set the header values 
  4. header("Content-Type: application/force-download\n");
  5. header("Content-Disposition: attachment; filename=".$_GET['fileName']);
  6.  
  7. //set the value of the fields in Opened dailog box
  8. header('Content-Disposition: attachment; filename="'.$_GET['fileName'].'"');
  9.  
  10. // echo the content to the client browser
  11. readfile($_GET['fileName']);
  12.  
  13. ?>
  14.  
  15.  
NOTE :
this script is so simple to teach the way,and its OUT OF SECURITY now.

so easy ;)

best regards.
mh
Mar 4 '07 #4

P: 1
i believe this is the easiest way to do so:

make a page that generates the file you want just like you would do with ajax (unless this is a fixed file which makes it even easier) and simply set a button with javascript code: "window.open(location);"
and it will download the file!

if you need to send information through a post method then you should tell the ajax element to generate the file and when it finished you will open it again with the same command "window.open(location);"
afterwards you can delete it with another ajax request.

its the easiest way in the word!
Jun 4 '09 #5

Post your reply

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