473,320 Members | 2,080 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,320 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 103023
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.