Attaching a File to a New Job in Script
4 min
Overview
You can attach a file to a job before submitting it by creating a JobAttachment from a file stream and adding it to the job's attachment collection.
Use Case
Use this approach when a script needs to create a new job and include one or more file attachments as part of the submission.
Example
using System.IO;
Job j = new Job();
j.Task = Evaluator.GetObject("=//Tasks/Log a Call") as Task;
string filePath = @"c:\temp\myreport.csv";
byte[] file = File.ReadAllBytes(filePath);
using (MemoryStream ms = new MemoryStream(file))
{
JobAttachment ja = JobAttachment.Create(
"activateextract.csv",
"application/text",
file.Length,
ms
);
j.Attachments.Add(ja);
j.Submit();
}How It Works
- Create a new Job object.
- Assign a task to the job.
- Read the file into a byte array.
- Create a MemoryStream from the file contents.
- Create a JobAttachment using the stream.
- Add the attachment to the job.
- Submit the job.
Key Points
- JobAttachment.Create() creates an attachment from a stream.
- The attachment must be added to the job's Attachments collection before submission.
- A task must be assigned to the job before calling Submit().
- The attachment filename does not need to match the source file name.
- The MIME type should reflect the file being attached.
Notes
- The example uses a MemoryStream to provide the attachment content.
- Ensure the file exists and is accessible before attempting to read it.
- Consider adding error handling to manage missing files, access issues, or submission failures.