7

I want to download a file when I click on a link. The file is served dynamically.

<a href="/server/myfunc?id=XXX" download="proposed_file_name">Download!</a>

But I need to be able to display error to the user if /server/myfunc?id=XXX link does not provide a file. I can return a boolean false or any http code through this link if file isn't present.

What should be the most appropriate method for this implementation. Note that I do not wish to alter the current state of page in any manner, just trigger the file download without affecting the page. And display error if file is not to be downloaded.

crazydiv
  • 812
  • 9
  • 30

2 Answers2

0

You can't do that only with html as far as it is a static language. I would suggest you to use a 'if/else' clause written in php or java. Here you have some documentation, and a concrete sample here. Anyway here you are a sample that could fit exactly on your case (taking into account you are inserting the php on the html).

<?php 
var = url
if (isset($var) && ($var === true){ ?>
<a href="/server/myfunc?id=XXX" download="proposed_file_name">Download!</a>
<?php } else { ?>
<p>Hey, there is no URL!</p>
<?php } ?>
Abueesp
  • 121
  • 1
  • 1
  • 9
0

I know I am replying very late, But it maybe useful for someone facing this kind of issue.

When using Synchronous HTTP calls, Its very difficult to show error Message as it would replace whole web page instead which we dont want.
So, Its better to use AJAX

Click here JohnCulviner Blog for more details - how to use AJAX for downloads

Also, Go through following SO link Download a file by query Ajax

Karan Kaw
  • 510
  • 1
  • 7
  • 15