How do I create an access-controlled download with Anvil?

I have a friend who’s in a band, and wants to sell downloads of his album at his gigs. So he will give out a bit of paper with a link to an app and an access code. They go to the link, enter the code, and if it’s a valid code they can download the file.

Hi Stuart,

You’ll want to put the file in Google Drive, then make a server module that only returns that file when given a valid code:

  • Add an app file into the Google service, mark it “no client access”.
  • Make a server function to check whether the code is valid (perhaps look it up in a data table?)
  • If the access code is valid, return the file from the server function. Returning a file from a server function overrides the “no client access” setting and gives the form code access to the file.
  • On the client, make a Link (say, ‘download_link’), and then, when the user enters an access code:
    file = anvil.server.call("get_file_if_code_valid", access_code)
    if file:
      self.download_link.url = file.get_url()
      self.download_link.visible = True
    

Hope that helps, let us know how you get on!

1 Like