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):
So the question is.....How do I get the browser to save this response as a zip file?
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Want to leave your mark on Confluence? One winner will have their whiteboard design added into Confluence for all users to access! This is your chance to showcase your creativity, help others, and gain recognition in the Confluence community.
Share your template today!Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.