I'm relatively new to all this so go slow. But what I'm trying to do is have a button where the user can click it and download a file from my site to their computer. I don't know if you need a script to do this or if their is some form command that I'm missing, this is the current code that I have...
Code:

<input type="button" value="Download" onClick="document.location='myfile.pdf'">

Problem is when people click the "Download" button all that happens is the PDF is displayed, they can then save it manually if they want. However what I'm wanting it to do is when they click the button it offers them the option to "View or Save As". As I write this it does sound more like a script than form code, however I thought I'd check with the pro's before I begin my goose chase.

Dani AI

Generated

Short answer: to reliably prompt a "Save As" dialog you must send the right HTTP headers from the server. As pointed out, the browser or PDF plugin can control what the user sees, but the correct server-side header is the way to request a download.

A minimal PHP approach (server script serves the file and sets headers):

<?php
$file = '/path/to/myfile.pdf';
if (!is_file($file)) { header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found'); exit; }
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: '.filesize($file));
readfile($file);
exit;
?>

That tells the browser to treat the response as a file to download. For large files avoid loading the whole file in PHP memory; use X-Sendfile / X-Accel-Redirect where available or stream the file in chunks. See the PHP readfile notes for details: PHP readfile(). The Content-Disposition header is documented here: Content-Disposition (MDN).

If you prefer a client-side-only option for simple, same-origin files, the HTML5 download attribute on an anchor is easy:

<a href="/files/myfile.pdf" download>Save PDF</a>

Note: download has same-origin limitations and some in-browser PDF viewers may still open inline. MDN documents the caveats: a download.

Server-wide forcing (Apache) is possible with mod_headers:

<FilesMatch "\.pdf$">
  Header set Content-Disposition "attachment"
</FilesMatch>

Confirm behavior by checking response headers (for example, curl -I yourfile). If users still see inline PDFs, inspect browser plugins or proxy layers that may alter headers.

If a visitor to your site have pdf installed on their computer, or plugged into their browser, the browser will open the pdf file when you click on the link. The user then have a choice to "save" to their computer, or just "view" the file in the browser. Most users also know that if you right click on a downloadable link, they will have the option to "save as". Most other files, like mp3 or word doc files when you click the link, your browser automatically asks you if you want to "view" the file, or "save as". This function is a user enabled function, and depending on how the user to your website had set up their browser settings, will depend on if they can "save" something or first "view" something. The choice should remain to the user.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.