Download Attachment Using Ajax Php Script

Active3 years, 2 months ago

PHP lessons, projects, references, examples, scripts, live demo and download. With an attachment on form submission using PHP. Using jQuery, Ajax and PHP. This custom function or PHP mail attachment script is able to send a plain text email message together with a single attachment file. The attachment file has to be uploaded first or you can use a file which already exists on your web server. Starting file download with JavaScript. Getting the url for download.php and then. What about using DHTML to dynamically write out the download link (from the.

I have the following html code inside a form:

I already have a javascript function that handles the onsubmit for the form using ajax but without handling the uploaded files.

Hp scan software mac 10.7. This is the part of my javascript function that sends the data to the required php file:

There are many tutorials and questions online about attachments using ajax and php but none of them handles multiple files.My question is: what do I need to add to this function so that I send the attached files to the php file ? And what should I write in the php file in order to handle the files it receives?

unknown_111unknown_111

2 Answers

To upload multiple images use array:

and send the form data using ajax:

In php file use a loop( foreach i recommend) to save all attachments.

PoonamPoonam

There are quite a lot of questions that are almost duplicates, but they all suffer from creating a FormData object and then adding fields to it one-by-one … which fails to show how to handle multiple file inputs (and is a long-winded and fragile approach to the problem). Microsoft office 2010 deutsches sprachpaket.

Just pass the form element as the argument to the FormData object instead.

QuentinQuentin
686k80 gold badges930 silver badges1084 bronze badges

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

Active2 years, 7 months ago

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.

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/

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

Leniel Maccaferri
83.7k36 gold badges302 silver badges409 bronze badges
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.

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

Ajax Php Tutorial

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

Ajax Load Script

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.

Marc 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.

Ajax Php Example Code

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

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).

Ajax php exampleVilx-Vilx-

Pagination Using Ajax Php

63.9k73 gold badges240 silver badges384 bronze badges

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):

Php Ajax Tutorial

Using

Ajax Script Manager

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

Community
nikib3ronikib3ro
13.6k20 gold badges102 silver badges166 bronze badges

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

Comments are closed.