[英]IdHTTP or TFileStream file lock?
我的應用程序向用戶顯示論文列表(pdf 文件)。 他們可以點擊下載(通過TidHTTP
)並在 TWebBrowser 中顯示TWebBrowser
。 如果文件已經存在,它會跳過下載。 上次我玩這個項目時(2019 年秋季)這段代碼是有效的,但現在當我在 iPhone 上運行它時我遇到了問題。
症狀:第一個點擊的論文會下載,然后在TWebBrowser
中正常顯示。 任何后續論文的點擊都將下載(我可以知道,因為我可以在我的應用程序文檔文件夾中列出 *.pdf 文件)但無法顯示。 我捕獲了當我將TWebBrowser
指向帶有Form1->WebBrowser1->URL = "file://" + LFileName;
的文件時發生的錯誤。 . 錯誤是“找不到指定的文件”。 它在那里是因為我可以在上面列出目錄。
如果我終止該應用程序並重新啟動它,然后 go 返回並單擊之前單擊的其中一篇論文(未顯示),它可以正常打開並顯示在TWebBrowser
中。 這真的讓我覺得這是某種文件鎖定問題,因為文件存在。
這是代碼:
void showPaper()
{
// paperName (e.g. 22.pdf)
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;
} // end of download if block
if (FileExists(LFileName)) // have the file so open it
{
try
{
Form1->WebBrowser1->URL = "file://" + LFileName;
}
catch ( const Exception& e )
{
ShowMessage(e.Message);
}
ShowMessage(Form1->WebBrowser1->URL);
}
} // end of showPaper()
發生錯誤時捕獲的消息是(在運行 13.3 的 iPhone 上):
顯示Form1->TWebBrowser1->URL
的 ShowMessage 給出的是正確的:
我沒有正確關閉TFileStream
嗎? 我可以終止應用程序、重新啟動並查看文件這一事實讓我知道文件已正確下載。 另外,第一次通過代碼完全工作(下載然后顯示在TWebBrowser
中)。 只有在后續嘗試需要下載才能顯示時,才會出現“找不到文件”的問題。
編輯:現在我創建了一個TWebBrowser
的 TWebBrowser WebBrowser1 的克隆。 它可以顯示 pdf 但后來我不知道如何正確刪除它。
這是我創建它並顯示 pdf 的代碼:
if (FileExists(LFileName)) // have the file so open it
{
try
{
TWebBrowser *myW;
myW = new TWebBrowser(Form1->Panel3);
myW->Parent = Form1->Panel3;
myW->Align = TAlignLayout::Client;
myW->URL = "file://" + LFileName;
myW->Visible = true;
}
catch ( const Exception& e )
{
ShowMessage(e.Message);
}
}
這是我刪除它的嘗試:
TComponent *T;
T = Form1->Panel3->Components[0]; // myW is only thing on Panel3
T->Free(); // not working
// T->DisposeOf(); // did not work
EDIT2 :嘗試處理臨時TWebBrowser
:
我像這樣創建TWebBrowser
(它可以很好地顯示 pdf):
TWebBrowser *myW;
myW = new TWebBrowser(Form1->Panel3);
myW->Parent = Form1->Panel3;
myW->Align = TAlignLayout::Client;
myW->URL = "file://" + LFileName; // displays the pdf
myW->Visible = true;
然后我嘗試像這樣處理它但不起作用:
TComponent *T;
for (int i = 0; i < (Form1->Panel3->ComponentCount); i++) {
T = Form1->Panel3->Components[i];
if (TWebBrowser* TB = dynamic_cast<TWebBrowser*>(T)) {
Form1->Panel3->RemoveComponent(TB);
TB->Parent = nullptr;
TB = nullptr;
break;
}
}
}
我沒有收到任何錯誤,我只是無法加載第二個 pdf(仍然找不到該文件錯誤)。 我正在使用演員表,因為我無法訪問T->Parent
。
void showPaper()
{
// paperName (e.g. 22.pdf)
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;
} // end of download if block
//////// this gets around the wierd file lock issue with TWebBrowser
UnicodeString TFileName = System::Ioutils::TPath::Combine(System::Ioutils::TPath::GetDocumentsPath(), "dummy.pdf");
if (FileExists(TFileName)) {
TFile::Delete(TFileName); // delete it if present
}
TFile::Copy(LFileName, TFileName);
//--------------------------------------
if (FileExists(LFileName)) // have the file so open it
{
try
{
TWebBrowser *myW;
myW = new TWebBrowser(Form1->Panel3);
myW->Parent = Form1->Panel3;
myW->Align = TAlignLayout::Client;
myW->URL = "file://" + TFileName;
myW->Visible = true;
}
catch ( const Exception& e )
{
ShowMessage(e.Message);
}
ShowMessage(Form1->WebBrowser1->URL);
}
} // end of showPaper()
在我顯示文件並且用戶點擊關閉按鈕后,我這樣做是為了擺脫臨時的TWebBrowser
:
TComponent *T;
for (int i = 0; i < (Form1->Panel3->ComponentCount); i++) {
T = Form1->Panel3->Components[i];
//if (T->ClassName() == "TWebBrowser") {
if (TWebBrowser* TB = dynamic_cast<TWebBrowser*>(T)) {
Form1->Panel3->RemoveComponent(TB);
TB->Parent = nullptr;
TB = nullptr;
break;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.