Sending an e-mail with an attachment to a recipient using the file upload component and a send button

Hello,
I am currently having problems sending an e-mail via a button that attaches the document which was uploaded in the file upload component. Something like this:
image
If i directly use a onfileRevieve script on the file upload component everything works.
But i don’t know how to access the uploaded data in the fileUploadComponent through the button.

I had something in mind like following script for the button:
filePath = self.getSibling(“FileUpload”).event.file.name
if filePath != None:
fileName = self.getSibling(“FileUpload”).event.file.name
fileData = self.getSibling(“FileUpload”).event.file.getBytes()
smtp = “"
sender = "
"
subject = “New ignition feedback”
body = self.getSibling(“TextArea”).props.text
recipients = ["********@
******”]
system.net.sendEmail(smtp, sender, subject, body, 0, recipients, [fileName], [fileData])
I know that this doesn’t work but i hope it makes it a bit clearer to understand.

Best regards
Johannes

You’ll have to save the uploaded file in a temporary location, then retrieve it in the send button’s script. And have a mechanism to clean up the temporary location of abandoned uploads (never sent).

4 Likes

Thanks a lot. That worked…

Hello, Could you please elaborate on this? I am new to scripting and am having a hard time to build this. When you say, save to a temporary location, is it the gateway that you are referring to? and what script do we use to retrieve and clean up? In my application, I have two buttons, one that allows me to attach file using "path = system.file.openFile()". I am not sure if I am approaching the issue in a right way. Kindly advice. Thank you.

Yes, all Perspective scripts run in the gateway, so the gateway is your only choice.

The simplest method is to use a gateway folder to hold these files, and remove them after the email is sent. Use a separate timer event to occasionally scan the folder for old files (by timestamp, perhaps) and delete them.

How can we remove the temp file?
I checked the temp file with system.file.fileexists but its returned as false!