string localPathAndFileName = "<Local file system path and file name for downloaded file>";
string folderId = "<Target folder Id for the upload>";
DocumentCreateBeginResult dcbr = null;
try
{
string localFileName = System.IO.Path.GetFileName(localPathAndFileName);
Stream localFileToBeUploaded = new MemoryStream(File.ReadAllBytes(localPathAndFileName));
//An SCMDocument needs to be created, it requires Name, ParentFolderId, and FileSize to be populated
SCMDocument document = new SCMDocument
{
Name = localFileName,
ParentFolderId = folderId,
FileSize = localFileToBeUploaded.Length
};
//Here we upload, in most cases, this code should always be used exactly as shown
dcbr = springCMService.DocumentCreateBeginWithFolder(token, document, null);
//Always use the chunk size from the API, do not set your own
int chunkSize = dcbr.ChunkSize;
int index = 1;
// Upload a chunk of the file at a time, looping until we are done
while (true)
{
//Calculate the next chunk to upload
//We cast to a long to accommodate for large files
long offset = (long)(index - 1)*chunkSize;
long remaining = localFileToBeUploaded.Length - offset;
//Stop processing, at the end of the file
if (remaining < 1)
{
break;
}
//If the remaining is less then the chunksize, send it, otherwise send a full chunk
int length = (int) (remaining >= chunkSize ? chunkSize : remaining);
byte[] buffer = new byte[length];
//Ok, read the next chunk and send it
//The FileIdentifier tells the API what file we are uploading
localFileToBeUploaded.Read(buffer, 0, length);
springCMService.DocumentCreateUploadChunk(token, dcbr.FileIdentifier, index, length, buffer, null);
//Increment chunk index
index++;
}
//All chunks have been uploaded, time to make the final commit call
//A new SCMDocument is returned that is updated with the Id
//Here additional properties can be added as well before commit such as Description, Metadata, etc.
document = springCMService.DocumentCreateCommit(token, dcbr.FileIdentifier, document);
Console.WriteLine("New Document created with Id {0}", document.Id);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
//Cancel the upload if we hit an exception
if (dcbr != null)
springCMService.DocumentCreateCancel(token, dcbr.FileIdentifier);
}