private void DownloadFileInParts(Document.File file, DocumentService docSvc, string outputfile) { int MAX_BUFFER_SIZE = 1024 * 1024 * 4; // 49 MB buffer size System.IO.FileStream outputStream = null; try { long startByte = 0; long endByte; // create the output file outputStream = System.IO.File.OpenWrite(outputfile); // for each loop, the MAX_BUFFER_SIZE number of bytes gets downloaded from the server and written // to disk while (startByte < file.FileSize) { byte[] buffer; endByte = startByte + MAX_BUFFER_SIZE; if (endByte > file.FileSize) endByte = file.FileSize; // grab the file part from the server buffer = docSvc.DownloadFilePart(file.Id, startByte, endByte, true); // write the data to the file outputStream.Write(buffer, 0, buffer.Length); startByte += buffer.Length; } } finally { if (outputStream != null) outputStream.Close(); } }