I’m trying to save the files associated with an InfoObject to disk. When the code hits the StreamingFile.NextChunk property, it raises a ‘File not open yet, please open file first’ exception. What am I missing?
try
{
string query = String.Format("SELECT * FROM ci_infoobjects WHERE si_id={0}", ObjectId);
InfoObject infoObject = Helper.GetInfoObjects(session, query)[1];
// get the first file (for testing)
CrystalDecisions.Enterprise.File attachment = infoObject.Files[1];
// get the extension; remove the '.'
string ext = Path.GetExtension(attachment.Name).Remove(0);
// create path (e.g. C:\Users\USERNAME\Desktop\ReportName.rpt)
string filePath = String.Format("{0}\\{1}.{2}", path, infoObject.Title, ext);
// create file (SUCCESS)
using ( FileStream fileStream = new FileStream(filePath, FileMode.Create) )
{
// open FRS file
using (attachment.StreamingFile)
{
// get file's content chunk by chunk
byte[] buffer;
while ((buffer = (byte[])attachment.StreamingFile.NextChunk) != null) // <-- exception generated here
{
// write to destination
fileStream.Write(buffer, 0, buffer.Length);
}
}
}
} // try
catch (Exception e)
{
throw e;
}
craibuc (BOB member since 2009-10-19)