0

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.

2
  • Thanks for your 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 that specified destination . Commented Sep 6, 2010 at 9:21
  • I've moved your above comment into the question, as that seemed like a better place to put it. Commented Sep 6, 2010 at 9:29

2 Answers 2

1

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';"
Sign up to request clarification or add additional context in comments.

3 Comments

well i have tried your suggestion but when i load that page it automatically downloads the whole web page as a txt file even before you click the save button
@fon: sounds like you're using chrome...? for chrome (and possibly other browsers), this is the default behavior for downloading files. it automatically drops it in the Downloads folder (unless the attachment is of a potentially unsafe type). IE and Firefox (I think) will prompt the user for where to save it. Either way, it'll always behave exactly like the users of the given browser expects it to, as it is entirely up to the browser to decide how to deal with attachments.
thanks very much man , it works perfectly with ff and ie and chrome it downloads it immediately. greate
0

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?

1 Comment

Thanks for your 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 that destination .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.