473,468 Members | 1,345 Online
Bytes | Software Development & Data Engineering Community
Create 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?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 1708

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...
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...
1
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...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
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 ...

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.