Add a File Attachment

Adds a file attachment.

Attachments provide a method to store more data alongside a Librarian document.

When you modify the attachments that are associated with a specific file version, the file version is not changed, and a new file version is not created. Because of this, attachments are similar to the metadata that is associated with a Librarian file, although attachments are used more often than metadata is for storing payloads. The FileHistory class provides a list of attachments that are associated with a file.

If you try to add an attachment that has the same name as an existing attachment, an error is returned.

Note: For more information about this command, including descriptions of any path and request parameters, request body schema, response codes and schemas, and payload examples, refer to the online API reference for this command at the following web page:

Add a file attachment

URI

https://bartendercloud.com/api/librarian/files/{fileId}/attachments

Note: The bartendercloud.com domain that is referenced in the URI and in the code example that follows must be replaced with the contents of the DataCenterURI claim that is extracted from your access token. For more information, refer to Extracting the DataCenterURI Claim.

Path Parameters

fileId

Type: String

Specifies the ID of the target file. Required.

Query Parameters

fileVersionMajor

Type: Integer <int32>

Specifies the requested major version. Optional.

fileVersionMinor

Type: Integer <int32>

Specifies the requested minor version. Required when fileVersionMajor is present; otherwise optional.

Example

Request body: System.Net.Http.MultipartFormDataContent

Response: See Attachment.

See also: AttachmentAddRequest, FileChange (provides a list of attachments that are associated with a file)

Copy
// Assume that the stream is of the System.IO.Stream type and references the file content to upload.
// Assume that fileId is the ID of the target file. 
var request = new AttachmentAddRequest() {
   Name = "SQL Attachment",
   Comment = "Attach SQL file Sample",
   ContentType = "sql",
   CopyForward = true    // Automatically include this attachment when a new version of the file is created. 
};
var client = new HttpClient();
StreamContent streamContent = new StreamContent(stream);
MultipartFormDataContent multipartFormDataContent = new MultipartFormDataContent
      {
         { new StringContent(JsonConvert.SerializeObject(request)), "attachmentAdd" },
         { streamContent, "formAttachment", attachmentAddRequest.Name }
      };

var msg = await client.PostAsync($"https://bartendercloud.com/api/librarian/files/{fileId}/attachments?fileVersionMajor={majorVersion}&fileVersionMinor={minorVersion}"
     multipartFormDataContent);

if (msg.IsSuccessStatusCode)
{
   return JsonConvert.DeserializeObject<Attachment>(await msg.Content.ReadAsStringAsync());
}