fon
fon

Reputation: 265

how to open the save as dialogue box with javascript

how can I programm a button so that when clicked should open the save as dialogue box?

Put simply, how can I save a file on the local machine using javascript or php? I really don't know how to go about it.

Thanks in Advance.

EDIT:

Thanks for the quick response. I have a table that I have displayed on a web page, and a button, so that when that button is clicked, the user can specify the file path, and save that table as a .txt file to the specified destination.

Upvotes: 0

Views: 1835

Answers (2)

David Hedlund
David Hedlund

Reputation: 129782

In PHP, you can set the Content-Disposition header to Attachment, which will tell the browser that the content being served is an attached file, which is to be downloaded:

<?php
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile('original.pdf');
?>

EDIT

To better fill the requirements of your updated question, you want to create a PHP-file that serves the table in the format you want it to be downloaded.

<?php
    header('Content-type: text/plain');
    header('Content-Disposition: attachment; filename="downloaded.txt"');

    echo " entire table here ";
?>

And then have your save button point to that pdf file

onclick="location.href='downloadTable.php?tableID=5';"

Upvotes: 1

Paddy
Paddy

Reputation: 33857

If you have a link on your page that points to a file that you are downloading from your site, and the browser doesn't recognize it, then you'll get the save as dialog appearing. Are there specific requirements that you have where you can't do this?

Upvotes: 0

Related Questions