TFileStream file lock still present?

  

The following code downloads a PDF file and then displays it in a TWebBrowser (on iOS). If the file is already present it of course just displays in the TWebBrowser. This works fine on the 1st file I try. But the 2nd attempt (and any subsequent) give me the following error message:

However, if i kill/restart the app and try that 2nd file first it immediately opens in the TWebBrowser (without downloading because the pdf file is already there). This makes me pretty sure that it is a file lock problem or similar. The file is getting downloaded but must still be hung in the TFileStream? I can do a directory listing on my apps Documents folder and i see the pdf files there. Just get that error when i try to open in the TWebBrowser unless i kill the app first. After doing that, i can open any of the downloaded pdf files fine. Note: My code tries to download via IPV4 first, and then by IPV6 since Apple requires both to work.

void showPaper()
{
// paperNAME has the pdf filename

if(paperNAME.Length() > 1) {

UnicodeString LFileName = System::Ioutils::TPath::Combine(System::Ioutils::TPath::GetDocumentsPath(), paperNAME);
if (!FileExists(LFileName)) { // file is not present so download it
UnicodeString URL = pdfURLv4 + paperNAME;
TFileStream* fs = new TFileStream(LFileName, fmCreate);
Form1->Download->ConnectTimeout = 15000; // give it 15 seconds
Form1->Download->ReadTimeout = 15000;
Form1->Download->Request->BasicAuthentication = true;
Form1->Download->Request->Username = “XXXXXX”;
Form1->Download->Request->Password = “YYYYYY”;
Form1->Download->Request->UserAgent = “Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0″;
try
{
Form1->Download->Get(URL, fs);
Form1->Download->Disconnect(); // make sure socket is closed
}
catch(const System::Sysutils::Exception &)
{
try
{
UnicodeString URL = pdfURLv6 + paperNAME; // the v6 url has brackets [] around host
Form1->Download->Get(URL, fs);
Form1->Download->Disconnect();
}
catch(const System::Sysutils::Exception &)
{
ShowMessage(L”No/poor internet connection.”);
Form1->Download->Disconnect();
delete fs;
return;
}
}
delete fs;
}

if (FileExists(LFileName)) // have the file
{
Form1->WebBrowser1->URL = “file://” + LFileName;
}

}else{ // paperNAME length 0
ShowMessage(“Paper not available for download.”);
}

} // end of showPaper()

Comments are closed.