473,396 Members | 1,900 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,396 software developers and data experts.

File download Ajax/PHP

ak1dnar
1,584 Expert 1GB
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 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. }
[PHP]
<?php
$fid = $_REQUEST['fid'];
require 'dbcon.php';
$sql="SELECT p_man FROM products where p_id = '$fid'";
$result=mysql_query($sql) or die("Error Occured while Searching Records : " . mysql_error());
$row = mysql_fetch_assoc($result);
$file = $row['p_man'];
if($file)
{
header("Content-Type: application/octet-stream");
header("Content-Length: " . filesize($file));
header('Content-Disposition: attachment; filename="'.$file.'"');
readfile($file);
}
?> [/PHP]
Mar 15 '07 #1
0 1706

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

Similar topics

3
by: Yi Chen | last post by:
We have a drop down list on a PHP page, with several product names, and when people click one item, we will refresh the same page with the product name as parameter, and in turn we want to include...
3
by: fnustle | last post by:
I've embedded a flash file into an assembly: But when I try to reference it on a web page: <object codebase="http://download.macromedia.com/pub/shockwave/ cabs/flash/swflash.cab"...
4
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.
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
by: suresh_nsnguys | last post by:
Hi, I will allow users to download one zip file(contain HTML file and 1 image directory with -gif,jpeg files) to his local hard disk. HTML file contain source code to display images ...
21
nathj
by: nathj | last post by:
Hi, I am currently working on a new site that offers various files for download. The file information is stored in a MySQL database and the page is produced in PHP depending on how the user got...
1
by: ma | last post by:
Hello, I developed an application which uses AJAX version 1.0. Now I want to deploy it in another computer and I have problem as I don't have AJAX support on the new computer and I don't where I...
2
by: BobF | last post by:
<given> When starting a new AJAX Control enabled site, a set of files are created. These are created -without- a master page. </given> To add a master page, I've been adding the master via...
0
by: =?Utf-8?B?aUhhdkFRdWVzdGlvbg==?= | last post by:
Thank U Sir, It works ::)) Now I have one more issue. I have downloaded Ajax frame work and in the toolbox I am getting AJAX Extention in which i find the Script manager. Bit,in the design...
1
Ciary
by: Ciary | last post by:
hey all, i have another problem. basically the question is: how do i make sure a file is downloaded instead of opened? but i'll explain the situation. i'm trying to download a file using a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...
0
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...
0
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
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,...

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.