Download Attachment Using Ajax Php Script

Posted By admin On 26.01.20
Active2 years, 7 months ago

That PHP code should then allow the user to download the file directly. What you're attempting is absolutely possible, you just need to approach it from another direction, ie not with AJAX. Share improve this answer. That PHP code should then allow the user to download the file directly. What you're attempting is absolutely possible, you just need to approach it from another direction, ie not with AJAX. Share improve this answer. Download a file by jQuery.Ajax. The download file prompt and using jquery ajax.and this solution. Get data using js so then no php would be needed. Send an E-Mail with Attachment Using jQuery, Ajax and PHP without Refrshing a Page, send email using jquery, ajax and php. Download Premium Only Scripts & 80+ Demo.

Let's say I have download links for files on my site.

When clicked these links send an AJAX request to the server which returns the URL with the location of the file.

Here's the function inside a complete download script. Attachment'); By using the HTTP. If you send a URL query like so localhost/download.php?filename.

What I want to do is direct the browser to download the file when the response gets back. Is there a portable way to do this?

VasilVasil
17.3k25 gold badges80 silver badges108 bronze badges

12 Answers

Try this lib https://github.com/PixelsCommander/Download-File-JS it`s more modern than all solutions described before because uses 'download' attribute and combination of methods to bring best possible experience.

Explained here - http://pixelscommander.com/en/javascript/javascript-file-downliading-ignore-content-type/

Usage: Press the download button above. The zip file contains all the code you need for the form. Unzip the file php-contact-form-with-file-attachment-script. Learn how to force a download using PHP, a BluDice article. A force-download script can give you more control over a file download than you would have providing a direct link. PHP Force Download – Keep Track of What’s Going Down. I´ve discovered the same strange ajax-problem while using a force-download php script.

Seems to be ideal piece of code for starting downloading in JavaScript.

Leniel Maccaferri
83.7k36 gold badges302 silver badges409 bronze badges
Ajax
PixelCommanderPixelCommander

We do it that way:First add this script.

Place this where you want the download button(here we use just a link):

The file 'download.php' (needs to be put on your server) simply contains:

So when you click the link, the hidden iframe then gets/opens the sourcefile 'download.php'. With the path as get parameter. We think this is the best solution!

It should be noted that the PHP part of this solution is a simple demonstration and potentially very, very insecure. It allows the user to download any file, not just a pre-defined set. That means they could download parts of the source code of the site itself, possibly containing API credentials etc.

Tom Busby
7701 gold badge6 silver badges22 bronze badges
roulioroulio

I have created an open source jQuery File Download plugin (Demo with examples) (GitHub) which could also help with your situation. It works pretty similarly with an iframe but has some cool features that I have found quite handy:

  • User never leaves the same page they initiated a file download from. This feature is becoming crucial for modern web applications
  • Tested cross browser support (including mobile!)
  • It supports POST and GET requests in a manner similar to jQuery's AJAX API
  • successCallback and failCallback functions allow for you to be explicit about what the user sees in either situation
  • In conjunction with jQuery UI a developer can easily show a modal telling the user that a file download is occurring, disband the modal after the download starts or even inform the user in a friendly manner that an error has occurred. See the Demo for an example of this.
John CulvinerJohn Culviner
16k5 gold badges40 silver badges44 bronze badges

Reading the answers - including the accepted one I'd like to point out the security implications of passing a path directly to readfile via GET.

Operation Flashpoint Cold War Crisis free download video game for Windows PC. Download free full version “Far Cry 2” from Gameslay. The game setup is tested and 100% fully working PC Game for free Download. Jun 14, 2016  Operation Flashpoint Cold War Crisis Game free Download Full Version. On-foot gameplay and the vehicle elements are blended seamlessly and the player can get into any available vehicle at any time, orders and mission conditions permitting. Operation flashpoint cold war crisis download free full version. Operation Flashpoint Cold War Crisis Game File Size:296.98 MB System Requirements! OS:Windows Xp,7,Vista,8 Ram:64 MB Video Memory: 8 MB CPU:Intel Pentium @ 266 MHz Hard Space: 300 MB Free Direct X:8.0 Sound Card:Yes A.I.M.2 Clan.

It may seem obvious to some but some may simply copy/paste this code:

So what happens if I pass something like '/path/to/fileWithSecrets' to this script?The given script will happily send any file the webserver-user has access to.

Please refer to this discussion for information how to prevent this: How do I make sure a file path is within a given subdirectory?

Community
DanielKhanDanielKhan
9821 gold badge8 silver badges18 bronze badges

If this is your own server application then i suggest using the following header

This will force any browser to download the file and not render it in the browser window.

fasih.ranafasih.rana
1,3451 gold badge11 silver badges22 bronze badges

Just call window.location.href = new_url from your javascript and it will redirect the browser to that URL as it the user had typed that into the address bar

GarethGareth
96.7k29 gold badges138 silver badges149 bronze badges

A agree with the methods mentioned by maxnk, however you may want to reconsider trying to automatically force the browser to download the URL. It may work fine for binary files but for other types of files (text, PDF, images, video), the browser may want to render it in the window (or IFRAME) rather than saving to disk.

If you really do need to make an Ajax call to get the final download links, what about using DHTML to dynamically write out the download link (from the ajax response) into the page? That way the user could either click on it to download (if binary) or view in their browser - or select 'Save As' on the link to save to disk. It's an extra click, but the user has more control.

Using ajax with phpMarc NovakowskiMarc Novakowski
38.8k8 gold badges52 silver badges60 bronze badges

To get around the security flaw in the top-voted answer, you can set the iframe src directly to the file you want (instead of an intermediate php file) and set the header information in an .htaccess file:

Matt KMatt K
4,9592 gold badges28 silver badges51 bronze badges

I suggest to make an invisible iframe on the page and set it's src to url that you've received from the server - download will start without page reloading.

Or you can just set the current document.location.href to received url address. But that's can cause for user to see an error if the requested document actually does not exists.

maxnkmaxnk
5,5622 gold badges23 silver badges20 bronze badges

In relation to the top answer I have a possible solution to the security risk.

Using the glob() function (I tested the download file in a path one folder up from the file to be downloaded) I was able to make a quick array of files that are 'allowed' to be downloaded and checked the passed path against it. Not only does this insure that the file being grabbed isn't something sensitive but also checks on the files existence at the same time.

~Note: Javascript / HTML~

HTML:

and

Pagination Using Ajax Php

JavaScript:

James Bordine IIJames Bordine II

I'd suggest window.open() to open a popup window. If it's a download, there will be no window and you will get your file. If there is a 404 or something, the user will see it in a new window (hence, their work will not be bothered, but they will still get an error message).

Vilx-

Php Ajax Tutorial

Vilx-
63.9k73 gold badges240 silver badges384 bronze badges

Ajax Script Manager

Why are you making server side stuff when all you need is to redirect browser to different window.location.href?

Here is code that parses ?file= QueryString (taken from this question) and redirects user to that address in 1 second (works for me even on Android browsers):

If you have jQuery in your project definitely remove those window.onpopstate & window.onload handlers and do everything in $(document).ready(function () { } );

Community
nikib3ronikib3ro

Download Attachment Using Ajax Php Scripts

13.6k20 gold badges102 silver badges166 bronze badges

Ajax Php Example Code

Not the answer you're looking for? Browse other questions tagged javascriptajaxdownload or ask your own question.