Retrieve Files from Documents |
Scroll |
A Thereforeā¢ document may contain one or more documents (e.g. 2 word documents, a PDF and 3 JPEG images), usually belonging to the same topic or task. These individual documents in a Thereforeā¢ document are often referred to as streams. The Step-by-Step guide illustrates how to extract one or all of these streams from a document via the Web API.
To retrieve streams from the Therefore document you can use the GetDocument operation (with the parameter IsStreamsInfoAndDataNeeded set to true) or the GetDocumentStream operation if you need the data of one stream only.
1 |
Create a channel factory to the web service endpoint. |
2 |
Create a channel to the web service endpoint. |
3 |
Create request parameters instance. Set requested document number and IsStreamsInfoAndDataNeeded parameter to true. |
4 |
Retrieve the document from the server. |
5 |
Close the channel and channel factory. |
6 |
Extract all file streams to the specified directory. |
// 1. Create a channel factory to the web service endpoint
...
ChannelFactory<IThereforeService> channelFactory = new ChannelFactory<IThereforeService>(binding, endpoint);
// 2. Create a channel to the web service endpoint
IThereforeService service = channelFactory.CreateChannel();
// 3. Create request parameters
GetDocumentParams parameters = new GetDocumentParams();
parameters.DocNo = = 123; // TODO: specify document number here
parameters.IsStreamsInfoAndDataNeeded = true;
// 4. Retrieve the document from the server
GetDocumentResponse response = service.GetDocument(parameters);
// 5. Close the channel and channel factory.
((IClientChannel)service).Close();
channelFactory.Close();
// 6. Extract all file streams to the specified directory
string extractDir = "D:\\temp\\";
string output = string.Empty;
foreach (var streamInfo in response.StreamsInfo)
{
string extractFileName = Path.Combine(extractDir, streamInfo.FileName);
File.WriteAllBytes(extractFileName, streamInfo.StreamData);
output += String.Format("Document stream extracted to {0}{1}", extractFileName, Environment.NewLine);
}