473,569 Members | 2,490 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 103069
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:hidd en,
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:hidd en,
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(lo cation);"
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(lo cation);"
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
6098
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 downloads. It uses fpassthru() and some headers() to send a file to the requesting user. The get.php file that I wrote (the file download module if you...
0
2568
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 http://www.site.com/download This download page contains some textboxes, checkboxes, radiobuttons, a DOWNLOAD BUTTON and text 3. I check some...
0
1819
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 window. Well, thanks to Steve Orr, I have that working. However, I still have a few issues. First, the download dialog does not have the name of the...
0
1733
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, identifies the template required then uses Response.AppendHeader to amend the response to create the file download dialogue. On my local machine this works...
18
16379
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 completely automatically what, until now, I do manually, witch is describe below : 1. I launch IE (6)
3
2125
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. The problem is that when you run the program it gets to the: Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name)
1
5694
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 ajax when i process PHP file i can open the download box to save the file. example: http://localhost/website/download.php?fid=1005 but when i am...
1
47390
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 on a link and after a moment or two a file download dialog box pops-up in your web browser and prompts you for some instructions, such as “open” or...
3
7344
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 before it works ??PLEASE help me with some code . Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8132
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7678
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7982
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3656
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1226
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.