Use a form to download a file from a URL

Here’s a website: Download link generator

And here’s the form in the page source:

<form id="download" class="download_action" method="POST">
<input type="hidden" name="action" value="download_action">
<label for="url">Please enter the URL you would like to save as a file:</label>
<div class="field">
<input id="url" class="text mr-3" name="keyword" type="text">
<input class="submit" title="Save target as" type="submit" value="Save target as">
</div>
<div class="tool-target" style="padding-bottom: 10px;"> </div>
<div id="return"> </div>
<input type="hidden" id="_wpnonce" name="_wpnonce" value="5c9a47c6d6" /><input type="hidden" name="_wp_http_referer" value="/tools/download-link-generator" />
</form>

It seems to be a server-side solution, but I don’t have much information about servers and how they do it. I wonder how it works exactly. Can similar projects be built on GitHub Pages, or do I need paid hosting and a server plus a vast knowledge of running it?

Short hand?

Server receives form.
Server gets url.
Server probably checks some things.
Server gets file contents
Server sends appropriate header for “this is a download”
Server pastes file contents
Server sends to browser.

Why do this instead of just… going to the URL? shrug

I’m not clear what it is you want to do.
Are you wanting to replicate the “Download link generator” form and have your own like it?
Or are you wanting to simply create a link yourself that will force download?

Exactly — a link or a button.

It is possible to build similar projects on GitHub Pages, provided that the project does not require any server-side code. The main advantage of using GitHub Pages is that it does not require paid hosting or a server, making it more cost-effective. In terms of knowledge, some basic understanding of HTML, JavaScript and CSS is usually sufficient to develop projects on GitHub Pages. However, if the project requires server-side code, then a paid hosting provider and a server are necessary, as well as an in-depth understanding of running and managing a server.

I don’t know a reliable way of doing it purely with client side code. There is the download attribute that can be put in an anchor, but it won’t necessarily work.
Ordinarily if the anchor links to a file type that can be opened in the browser, that’s what happens by default. But the user has the option of right-clicking and choosing to “save as”.
To force a download you need to put that in the HTTP headers, which you will do from the server. for example in PHP you may do it like this:-
https://www.php.net/manual/en/function.header#example-4531

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.