473,569 Members | 2,402 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

File download Ajax/PHP

ak1dnar
1,584 Recognized Expert Top Contributor
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?fi d=1005

but when i am sending the fid (using a onclick event of a Image i am sending the ID to JS func ) through Ajax Script this PHP file will execute in server side. so can't retrieve those headers to the page i am sending the request. is there any way to Open this dialog box. issue is now the PHP fle is executing in server side, i have to call it in client side as i feel.

Expand|Select|Wrap|Line Numbers
  1. var xmlHttp;
  2. function GetXmlHttpObject()
  3. var objXMLHttp=null;
  4. if (window.XMLHttpRequest)
  5.  {
  6.  objXMLHttp=new XMLHttpRequest();
  7.  }
  8. else if (window.ActiveXObject)
  9.  {
  10.  objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
  11.  }
  12. return objXMLHttp;
  13. }
  14. function file_down(fid)
  15. {
  16. xmlHttp=GetXmlHttpObject();
  17.     if (xmlHttp==null)
  18.      {
  19.      alert ("Your Browser does not support AJAX, please Update!");
  20.      return;
  21.      } 
  22. var url="download.php?fid="+fid;
  23. url=url+"&zid="+Math.random();
  24. //xmlHttp.onreadystatechange=stateChanged_file_down;
  25. xmlHttp.open("GET",url,true);
  26. xmlHttp.send(null);
  27. }
  28. function stateChanged_file_down() 
  29. if (xmlHttp.readyState==4)
  30. // Can't guess what to do here....
  31. }
  32.  
download.php
[PHP]<?php
$fid = $_REQUEST['fid'];
require 'dbcon.php';
$sql="SELECT p_man FROM products where p_id = '$fid'";
$result=mysql_q uery($sql) or die("Error Occured while Searching Records : " . mysql_error());
$row = mysql_fetch_ass oc($result);
$file = $row['p_man'];
if($file)
{
header("Content-Type: application/octet-stream");
header("Content-Length: " . filesize($file) );
header('Content-Disposition: attachment; filename="'.$fi le.'"');
readfile($file) ;
}
?>[/PHP]
Mar 15 '07 #1
1 5694
acoder
16,027 Recognized Expert Moderator MVP
Use a hidden iframe or a normal form submit.
Jun 4 '08 #2

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)
4
103070
by: ssmith | last post by:
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.
0
1719
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
47391
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...
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
8138
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
7679
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
6287
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3657
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...
1
2117
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
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.