Search code examples
phplaraveldownload

Hide download link as soon as the download starts


Question: What do I have to do to hide a download link as soon as the download starts?

Problem: The difficulty or problem for me here is that a password is sent with the user request. If the password is correct, the download starts. If the password is incorrect, the download is redirected to the same page.

Blade / Frontend

<form action="/download/{{ $data->uuid }}" method="POST" class="download-with-pw">
  @csrf
  <label for="password">Password:</label>
  <input type="password" name="password" value="">
  <input type="submit" value="Download">
</form>

Controller / Backend

...
if ($request->input('password') === $file->password) {
  return \Illuminate\Support\Facades\Storage::download($path);
}

Solution

  • I expect you should just hide it by handling the "submit" event of the form using JS. It should run before the submission occurs.

    Rather than a "success" message, just put a "your download will start shortly" message, or something like that - that's pretty common on download sites.

    Then if it turns out there's an error, the server will redirect the browser back to a suitable point, which presumably will show the form again anyway.