code: def action_add(self):
          
  
          form = self.request.form
          parent = api.get_parent(self.context)
          attachment_file = form.get('AttachmentFile_file', None)
          AttachmentType = form.get('AttachmentType', '')
          AttachmentKeys = form.get('AttachmentKeys', '')
          ReportOption = form.get('ReportOption', 'r')
  
          # nothing to do if the attachment file is missing
          if attachment_file is None:
              logger.warn("AttachmentView.action_add_attachment: Attachment file is missing")
              return
  
          # create attachment
          attachment = self.create_attachment(
              parent,
              attachment_file,
              AttachmentType=AttachmentType,
              AttachmentKeys=AttachmentKeys,
              ReportOption=ReportOption)
  
          # append the new UID to the end of the current order
          self.set_attachments_order(api.get_uid(attachment))
  
          # handle analysis attachment
          analysis_uid = form.get("Analysis", None)
          if analysis_uid:
              analysis = api.get_object_by_uid(analysis_uid)
              others = analysis.getAttachment()
              attachments = []
              for other in others:
                  attachments.append(other.UID())
              attachments.append(attachment.UID())
              analysis.setAttachment(attachments)
              # The metadata for getAttachmentUIDs need to get updated,
              # otherwise the attachments are not displayed
              # https://github.com/senaite/bika.lims/issues/521
              analysis.reindexObject()
  
          else:
              others = self.context.getAttachment()
              attachments = []
              for other in others:
                  attachments.append(other.UID())
              attachments.append(attachment.UID())
  
              self.context.setAttachment(attachments)
  
          if self.request['HTTP_REFERER'].endswith('manage_results'):
              self.request.response.redirect('{}/manage_results'.format(
                  self.context.absolute_url()))
          else:
              self.request.response.redirect(self.context.absolute_url())
docs: Form action to add a new attachment
  
          Code taken from bika.lims.content.addARAttachment.