473,698 Members | 2,972 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. }
[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
0 1725

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

Similar topics

3
6893
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 a HTML file into the content area of the same page. I know it is recommended to put everything into database, but we want the web site to be very "portable", so the drop-downlist and the content should both in text files. Let's say the...
3
2989
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" height="100" width="100" classid="clsid:D27CDB6E- AE6D-11cf-96B8-444553540000">
4
103095
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
5731
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 sending the fid (using a onclick event of a Image i am sending the ID to JS func ) through Ajax...
1
1849
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 when user execute the 'HTML' file locally.I will display the images present in the local directory one by one in loop until user close the browser. I want to implement like this If i execute the 'HTML' file locally,first it needs to connect...
21
2465
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 to the page: <?php session_start(); $_SESSION = $_SESSION; // set to whatever is, as you can only access this page from within a top level section ?> <!--
1
2552
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 can download this version of AJAX. the Ajax.asp.net has the newer version of AJAX (3.5). Any help? Regards
2
1398
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 add-new-item. However, there is no automajic addition of references etc. to already existing forms. Now I have a few options:
0
1264
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 form I am not getting this part of the tag.. <ajaxToolkit:ToolkitScriptManager I also added AjaxControlToolkit.dll in the bin directory.
1
12808
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 button. when clicked, i call an ajax-request to a php-page where i dynamically create a ".pmp"-file. then it returns the name of the file. last, i open the file using document.location. here is my ajax request and the code to redirect my page....
0
8683
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9170
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8904
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7741
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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
3
2007
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.