You are here

You are here

Download Document - Native

When downloading a document via the SOAP API, the document must be "chunked" as it is retrieved from SpringCM.  The samples shown below show how to properly chunk a file out of SpringCM to a local file system.

Vertical Tabs

c#
string documentId = "<Document Id retrieved by other method calls>";
string localPathAndFileName = "<Local file system path and file name for downloaded file.>";
 
//First get the document, we need to do this to know the full file size 
//We can pass false for the last parameter since we won't need metadata 
SCMDocument document = springCMService.DocumentGetById(token, documentId, false);
 
//Create an in memory stream to write the downloaded document 
using (FileStream stream = new FileStream(localPathAndFileName, FileMode.Create))
{
 
	//Initialize the download variables 
	//position keeps track of where we are in downloading the file 
	//chunk size is how many bytes we ask SpringCM for at a time, in this 
	//case 1MB. 
	int position = 0;
	int chunkSize = 1048576;
 
	//The position increases until we have all the bytes 
	//so loop while there is still more to download 
	while (position < document.FileSize)
	{
		//The final chunk will likely be smaller than the chunksize 
		//so we check if the next 1MB chunk would take us past the 
		//bytes left to be downloaded. If this is the case, 
		//we change the chunksize to the final set of the bytes 
		//We have to check everytime in this loop to see when we get 
		//to the end. 
		if (position + chunkSize >= document.FileSize)
		{
			chunkSize = (int) (document.FileSize - position);
		}
 
		//Get the current chunk, and then write it to the stream 
		byte[] currentChunk = springCMService.DocumentDownload(token, document.Id, position, chunkSize, DownloadFormat.Native);
		stream.Write(currentChunk, 0, chunkSize);
 
		//Move the position to be ready for the next chunk 
		position += chunkSize;
	}
}
java
String documentId = "<Document Id retrieved by other method calls>";
String localPathAndFileName = "<Local file system path and file name for downloaded file.>";
 
//First get the document, we need to do this to know the full file size
//We can pass false for the last parameter since we won't need metadata
SCMDocument document = springCMService.documentGetById(token, documentId, false);
 
//Initialize the download variables
//position keeps track of where we are in downloading the file
//chunk size is how many bytes we ask SpringCM for at a time, in this
//case 1MB.  
int position = 0;
int chunkSize = 1048576;
 
//The position increases until we have all the bytes
//so loop while there is still more to download
ByteBuffer downloadedDocument = ByteBuffer.allocate((int)document.getFileSize());
 
while (position < document.getFileSize())
{
	//The final chunk will likely be smaller than the chunksize
	//so we check if the next 1MB chunk would take us past the
	//bytes left to be downloaded.  If this is the case,
	//we change the chunksize to the final set of the bytes
	//We have to check everytime in this loop to see when we get
	//to the end.
	if (position + chunkSize >= document.getFileSize())
	{
		chunkSize = (int)(document.getFileSize() - position);
	}
 
	//Get the current chunk, and then write it to the stream
	byte[] currentChunk = springCMService.documentDownload(token, document.getId(), position, chunkSize,DownloadFormat.Native);
	downloadedDocument.put(currentChunk, 0, chunkSize);
 
	//Move the position to be ready for the next chunk
	position += chunkSize;
 
}
 
FileOutputStream fileOutputStream = new FileOutputStream(localPathAndFileName);	   
fileOutputStream.write(downloadedDocument.array());
fileOutputStream.close();
php
 <?php 
$SpringCM = new SoapClient('https://soapna11.springcm.com/atlas/webservices/v201305/springcmservice.asmx?wsdl',array('trace'=>true));
$AuthResult = $SpringCM->AuthenticateNonDefault(array('userName' => '<your id>','password'  => '<your password>','AccountId' => '<your account id>','apiKey' =><your api key>' ));
 
$token = $AuthResult->AuthenticateNonDefaultResult;
 
//First get the document, we need to do this to know the full file size
//We can pass false for the last parameter since we won't need metadata
$documentId = '<Document Id retrieved by other method calls>';
 
$document = $SpringCM->DocumentGetById(array('token' => $token,'documentId'=>$documentId,'loadExtendedMetadata' => false));
$document = $document->DocumentGetByIdResult;
 
//Initialize the download variables
//Position keeps track of where we are in downloading the file
//chunk size is how many bytes we ask SpringCM for at a time--
//in this case 1MB.
$position = 0;
$chunkSize = 1048576;
 
//Create an in memory stream to write the downloaded document
$file = fopen("c:\\temp\\" . $document->Name,'w');
 
while($position<$document->FileSize)
{
    //The final chunk will likely be smaller than the chunksize
    //so we check if the next 1MB chunk would take us past the
    //bytes left to be downloaded.  If this is the case,
    //we change the chunksize to the final set of the bytes
    //We have to check everytime in this loop to see when we get
    //to the end.
    if ($position + $chunkSize >= $document->FileSize)
    {
        $chunkSize = $document->FileSize - $position;
    }
 
    //Get the current chunk, and then write it to the stream
    $downloadResult = $SpringCM->documentDownload(array('token' => $token, 'documentId' =>$document->Id,
           'start' => $position, 'length'=> $chunkSize,'format'=>'Native'));                                   
 
    fwrite($file, $downloadResult->DocumentDownloadResult);
 
    //Move the position to be ready for the next chunk
    $position += $chunkSize;
 
}
 
fclose($file);
?>