473,387 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

File Download with AJAX

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
4 103045
pronerd
392 Expert 256MB
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
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
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
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
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
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
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
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
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
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
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
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.