Upload a file
Note
When it comes to upload using REST based api calls, refer to the upload section in the REST documentation.
Overview
The following steps are required to upload a file to ImageVault:
- Upload file content
- Store content in vault
- Save and mark as organized
Select vault
To upload a file the user must first decide which Vault the media should be available for. To upload an item to a vault the user needs to have at least Contribute access to the vault. The following example retrieves the first vault where we have at least contribute access.
var vault = client.Query<Vault>().Where(v => v.CurrentUserRole == VaultRoles.Contribute).FirstOrDefault();
if (vault == null)
throw new ApplicationException("User has no access for uploading to any vault");
Upload contents
To upload a file we need to communicate with the IUploadService. The upload service has a UploadFileContent method that takes a stream and an id. You can either upload the whole file at once or you can upload the file in sequential parts by supplying the id of the file on each part. The id of the file is returned on the first call to the method.
var file = new FileInfo(@"C:\Users\dan\Pictures\plugin.png");
var uploadService = client.CreateChannel<IUploadService>();
string id;
using (var fs = file.OpenRead()) {
id = uploadService.UploadFileContent(fs, null);
}
Store in vault
When the whole contents of the file has been uploaded, we need to finalize the upload by calling the StoreContentInVault method
var mcs = client.CreateChannel<IMediaContentService>();
var mediaItem = mcs.StoreContentInVault(id, file.Name, "image/png", vault.Id);
Console.WriteLine("Uploaded media item " + mediaItem.Id);
Here we pass the id of the uploaded file, the name of the file, content type and the id of the vault where it should be stored.
It is important that the content type of the media is correct, since ImageVault will use this information to decide which converters/analyzers that should be used. ImageVault will not parse the files contents to determine the content type if omitted.
When the file is stored it needs to be marked as ready to use (it won't be listed in normal Find operations until it is ready to use). This step is intended for the uploader to perform organizational tasks before submitting it to the vault.
Mark item as ready to use
To mark a media item as ready to use we invoke the Save method and supply the MediaServiceSaveOptions.MarkAsOrganized to mark the item as ready to use.
var ms = client.CreateChannel<IMediaService>();
ms.Save(new List<MediaItem> { mediaItem }, MediaServiceSaveOptions.MarkAsOrganized);
Console.WriteLine("Media item marked as organized");
Now we have completed the upload procedure and the media item is ready to use