Add a File

Adds a file.

To add a file to a Librarian folder, you must know the parent folder's identifier.

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

URI

https://bartendercloud.com/api/librarian/spaces/{spaceId}/files

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

spaceId

Type: Integer <int32>

Specifies the ID of the requested space. Use the number 1 to target the Main space or the number 2 to target the $temp space. Required.

Query Parameters

None.

Example

Request body: System.Net.Http.MultipartFormDataContent.

Response: A FileChange object is returned that provides details about the file.

See also: FileAddRequest.

Copy
// Assume that the int variable spaceId=1 (this value references the Main space and is substituted into the URI below).
// Assume that the stream is of the System.IO.Stream type and references the file content to upload.
// Assume that folderId is the ID of the target folder. You can use the Get and GetByPath Folder APIs or the target space's RootFolderId property as a source for this value.

// Collect the details for the file add request.
var fileAddRequest = new FileAddRequest() {
   Name = "TestFile.txt",
   Comment = "Adding TestFile",
   Encryption = "",               // Not encrypted.
   FileContentType = "txt",
   InheritFromFolderPermissions = true,
   IsHidden = false,
   VersionDescription = "Initial Checkin",
   FolderId = folderId
};

// Bind the data stream for the file to the request.
StreamContent streamContent = new StreamContent(stream);
MultipartFormDataContent multipartFormDataContent = new MultipartFormDataContent
{
   { new StringContent(JsonConvert.SerializeObject(fileAddRequest)), "fileAdd" },
   { streamContent, "formFile", fileAddRequest.Name }
};

var client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken}");

var msg = await client.PostAsync($"https://bartendercloud.com/api/librarian/spaces/{spaceId}/files" multipartFormDataContent);

if (msg.IsSuccessStatusCode)
{
   // The returned FileChange includes the file's new fileID.
   return JsonConvert.DeserializeObject<FileChange>(await msg.Content.ReadAsStringAsync());
}