Have you looked at what is called by the out of the box attachments code? I expect you could use tracer or fiddler to figure out the necessary components if you want to do this custom. Although I'd also suggest taking a step back and asking if there was a way you could customize the out of the box UI so that it meets your business needs and you don't need to reinvent the wheel.
Could you please refer the Embed-Attach-File.pyAttachFieldOptional OOB section. In this section please go to the actions of Download link and verify the run script action. It may give you an clue to proceed further.