You are here

You are here

Uploading a new Document

Documents are uploaded to SpringCM in "chunks".  The code samples below show the proper way to chunk a file from a local file system to SpringCM. 

Vertical Tabs

c#
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);
}
java
String localPathAndFileName = "<Local file system path and file name for downloaded file>";
String folderId = "<Target folder Id for the upload>";
DocumentCreateBeginResult dcbr=null;
FileInputStream localFileToBeUploaded=null;
 
try
{
	File file = new File(localPathAndFileName); 
	String localFileName = file.getName();
	localFileToBeUploaded = new FileInputStream(file);
 
	//An SCMDocument needs to be created, it requires Name, ParentFolderId, and FileSize to be populated
	SCMDocument document = new SCMDocument();
	document.setName(localFileName); 
	document.setFileSize(file.length()); 
	document.setParentFolderId(folderId); 
 
        //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.getChunkSize(); 
	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 = file.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.getFileIdentifier(), 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.getFileIdentifier(), document); 
 
	System.out.format("New Document created with Id %s\n",document.getId());
}
catch(Exception ex)
{
	ex.printStackTrace(); 
	springCMService.documentCreateCancel(token, dcbr.getFileIdentifier());
}
finally
{
	if(localFileToBeUploaded!=null) 
	{
		localFileToBeUploaded.close();
	}
}