I have a custom object "CDOC" as a child of "Account".

This custom object "CDOC" has a a URL field called "DocLink".

This field contains a URL of a doc.

When a user clicks on this URL from his browser, this doc will be downloaded to his machine.

I would like to know if using Apex I will be able to download this file (available from this URL) or some other way to automate this ?

Has anyone done something like this (as I believe this problem is little unique) ?

  • 1
    You want to download the file, directly to their computer, without their knowledge or consent?
    – sfdcfox
    Jan 22, 2018 at 2:40
  • @sfdcfox to add further...a) URL field does not contain "http" or "https" instead it is a local machine in our network like "machine_name/filename" b) yes. i want my apex to copy/download the associated file stored in the local machine and download it to a location that i indicate. Is this possible via apex ? If not what tools are available for this use case ? Jan 22, 2018 at 2:57
  • what type of files and where are you going to store them?
    – Chris Chen
    Jan 22, 2018 at 3:01
  • 2
    The answer provided is as close as you'll get. The server-side Apex Code cannot interact with files on your local network or force a file to be saved to a local computer. There's simply no interface for this. You would need to use Visualforce or Lightning to accomplish your goal.
    – sfdcfox
    Jan 22, 2018 at 3:05
  • 1
    @ZenSeeker Yes, Apex Code does not know the name of your local machine, cannot access other boxes on your network, etc. Most browsers also directly prohibit accessing local files via scripting without user interaction, so the original manual solution is the solution. Having the ability to just drop random files on a computer has been pretty-well locked down to prevent spreading malware, randomware, etc. Theoretically, one could build a solution using Apex Code, Platform Events, a local listener, etc... but that's a lot of work to save a few clicks.
    – sfdcfox
    Jan 22, 2018 at 3:13

1 Answer 1


You can do this with a simple html a element. Just add target='_blank' and the browser will try to download it if it's a downloadable file such as .pdf or .doc.

Alternatively (and better) with HTML-5 you can use the download attribute of the a element, something like this:

<a href="{!v.CDOC.DocLink}" download="CustomFilename">Click here to download!</a>

  • I am interested in developing an apex solution (which I will execute either on demand or anon or trigger) for this...not an UI one. Jan 22, 2018 at 3:03
  • oh sorry I misunderstood a little because of the when a user clicks it
    – Aequitas
    Jan 22, 2018 at 3:04
  • @user2655970 not sure if it's possible since apex is server side, unless maybe if you give access to the target location, but I'm not sure on how to do it in the apex language
    – Aequitas
    Jan 22, 2018 at 3:05
  • are there any tools (not related to salesforce) which can help me download automatically ? Jan 22, 2018 at 3:10
  • @user2655970 if all you're interested in is downloading the files then there are plenty of ways to download a list of URLs. wget and curl are common ways to download a list of URLs
    – Aequitas
    Jan 22, 2018 at 3:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .