473,508 Members | 2,324 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

File Download with AJAX

1 New Member
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
4 103063
pronerd
392 Recognized Expert Contributor
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
28 New Member
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
28 New Member
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
sarussian
1 New Member
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

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

Similar topics

5
6081
by: Brandon Walters | last post by:
I wrote a file download module for my website. The reason for the file download module is that my website downloads work on a credit based system. So I need to keep track of and limit daily...
0
2564
by: jmd | last post by:
Hello. I want to write a C# program that does completely automatically what, until now, I do manually, witch is describe below : 1. I launch IE (6) 2. I browse to my desired download page, say...
0
1816
by: Buddy Ackerman | last post by:
I am trying to implment a file download via a link such that when clicked, instead of starting the default application for that type of file the user will be presented with a download dialog...
0
1729
by: Rhys666 | last post by:
Basically I have a link that opens my download page and the querystring identifies the type of 'template' Excel spreadsheet has asked to download. The download page reads the querystring,...
18
16367
by: jmd | last post by:
Hello, I posted the following in the C# forum but without one answer. But perhaps now in vb.net someone has some guidelines ! This is my question : I want to write a vb.net program that does...
3
2122
by: tshad | last post by:
I have a function that downloads a file to the users computer and it works fine. The problem is that I then want the program to rename the file (file.move) to the same name plus todays date. ...
1
5669
ak1dnar
by: ak1dnar | last post by:
Hi i need to download some files (.pdf/.doc) from web root. I am using a ajax script to send the file IDs and PHP will process it and it should generate the save as dialog box. Currently without...
1
47348
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
3
7332
by: hubertdennis | last post by:
hi every body i want to upload file using Ajax ,i have try to use this code bellow ,but it's not work (i can't write helloworld in text1.txt).Du you no what is a problem ??me i configure anithing...
0
7118
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.