How to download a file from webserver using ajax and php

Glen Collins February 1, 2022

Hello all! I'm having some issues trying to get a created zip file downloaded via the web browser and then deleted on the server after the download is complete. I was going to post my code here but I thought I'd get some NEW ideas how to do this without seeing my mess of code on here. So here is what I'm trying to do: (All servers are linux based)

1) User enters data in confluence page

2) Press create and download button on confluence page (using post ajax call to PHP page)

3) Create file on server side

4) Download and save the file to the local system

5) Delete the file on the server side

I have a confluence page with some form fields that a user has to enter data in. So I've been using ajax to submit a post request to a webserver on the confluence server running on a different port than confluence. The file gets created just fine and when it returns the data to the web browser I see the steam of data in the debugger but I cannot get it to save the file locally.

So then I switched to the method of opening a window with the URL to the file and that worked and saved the file. When I placed the unlink function in the php code the file was deleted BEFORE the download even started. So no file was ever downloaded.

I would prefer to delete the file using php than using cron or something of a hack. This seems simple but since I pretty much hack my way thru jquery/ajax/php this is kind of a hard one to diagnose.

If someone can point me to some sample code that would so what I'm looking for, it would be very much appreciated. I've look at so many pieces of code I'm sure I'm mixing stuff up.

Thanks.

Glen

Here is the code I'm currently using that creates the file and tries to send the steam to the browser for download.

Form and ajax code:

<form>

<input id="entry1" type="text" name="entry1">
<input type="text" name="entry2" value="Default Value">
<input type="text" name="entry3" value="Default Value 1">
<input type="text" name="entry4" value="Default Value 2">
<input type="text" name="entry5" value="Default Value 3">

</form>

<script type="text/javascript">

$(document).ready(function(){
$('#download').click(function(){
$.ajax({
url: 'https://server.domain.com:9443/create-file.php',
data: $('form').serialize(),
  type: 'post',
success: function(data){
// window.location = response;
$('#entry1').val(''); // Clear hostname entry after success
      }
});
});
});

</script>

PHP (Server side) Code:

<?php
header('Access-Control-Allow-Origin: *');
$field1 = $_POST["entry1"];
$field2 = $_POST["entry2"];
$field3 = $_POST["entry3"];
$field4 = $_POST["entry4"];
$field5 = $_POST["entry5"];

clearstatcache();

$filename = shell_exec("/usr/local/bin/script.sh -r $field3 -e '$field4' -d $field2 -o '$field5' -s '$field1'");

$newfilename = str_replace(PHP_EOL, '', $filename); // Sent back from bourne shell script - Remove EOL character

if (file_exists($newfilename)) // Does file exist?
{
header('Content-Description: File Transfer');
header('Set-Cookie: fileDownload=true; path=/');
header('Content-Type: '.mime_content_type($newfilename));
header('Content-Disposition: attachment; filename="'.basename($newfilename).'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: '.filesize($newfilename));
ob_clean();
flush();
readfile($newfilename);
/* unlink($newfilename); */

}

?>

Here is what's showing in the response tab of the debugger (zip file):

debugger.PNG

So the question is.....How do I get the browser to save this response as a zip file?

1 answer

0 votes
Pramodh M
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 1, 2022

Hi @Glen Collins 

While I don't specialize in Ajax or PHP

Heres the step you need to follow

You said you have created the page successfully!!

The next step is to get the page id by searching the Page Name - https://developer.atlassian.com/server/confluence/confluence-rest-api-examples/#find-a-page-by-title-and-space-key

The next step is to get the content of the page using page ID - https://developer.atlassian.com/server/confluence/confluence-rest-api-examples/#read-content--and-expand-the-body

Well reading the content is basically downloading!!

Let me know if you have any queries

Thanks,
Pramodh

Glen Collins February 2, 2022

Thanks for the info but this is not dealing with a confluence page itself as this is outside of confluence content. I'm using confluence as a way for the user to enter the data and download the resulting file.

I'm going to add my code and maybe someone can assist finishing the last piece I'm missing to actually download the resulting file.

Thanks

Glen

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events