Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

How to know if there is error when downloading files?

coater
Posts: 187
Joined: Sat Sep 29, 2018 1:51 pm

How to know if there is error when downloading files?

Post by coater »

Sometimes, for some reasons download error may happen. So how to know it, thank you!

------------------------------------
procedure TIinformSearchFrm.Chromium1DownloadUpdated(Sender: TObject;
const browser: ICefBrowser; const downloadItem: ICefDownloadItem;
const callback: ICefDownloadItemCallback);
begin
if downloadItem.IsComplete then
begin
IfNoDown:=true;
end;

end;
------------------------------------
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to know if there is error when downloading files?

Post by salvadordf »

Use these events :
  • TChromium.OnResourceResponse : Check the HTTP status in response.Status
  • TChromium.OnLoadError : Check if the "failedUrl" parameter is one of your downloads.
coater
Posts: 187
Joined: Sat Sep 29, 2018 1:51 pm

Re: How to know if there is error when downloading files?

Post by coater »

Thank you very much!
Another problem got me there:

Is there another way to download PDF (or acquire PDF doc url to download) when open a web embedded with PDF document?
( not set GlobalCEFApp.DisablePDFExtension to false/not copy file from catch.)
----------------------
ResposeHeader(from web):
cache-control:max-age=3600, private, must-revalidate
content-disposition:inline; filename=jr211.pdf
content-type:application/pdf; charset=UTF-8
date:Thu, 11 Oct 2018 00:06:50 GMT
pragma:
server:AtyponWS/7.1
set-cookie:JSESSIONID=aaat-iAt43LJj-uucg6yw; domain=.onlinelibrary.wiley.com; path=/
set-cookie:cookiePolicy=iaccept; expires=Fri, 12-Oct-2018 00:06:50 GMT
status:200
strict-transport-security:max-age=16070400
x-content-type-options:nosniff
x-frame-options:SAMEORIGIN
x-webstats-respid:a237cd931a4d34739c13754a69b8f5cf
x-xss-protection:1; mode=block
---------------------

Mine:
----------------------
procedure TMiniBrowserFrm.Chromium1BeforeBrowse(Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame;
const request: ICefRequest; user_gesture, isRedirect: Boolean;
out Result: Boolean);
begin

if ansipos('pdfKeyWord', Request.Url)<>0 then
begin
showmessage(Request.Url);
Chromium1.StopLoad;
Chromium1.StartDownload(Request.Url)
end;

end;
----------------------
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to know if there is error when downloading files?

Post by salvadordf »

Use the TChromium.OnResourceResponse event and check the content-type header in the response parameter.
If it contains "application/pdf" then you can copy the url from the request.
coater
Posts: 187
Joined: Sat Sep 29, 2018 1:51 pm

Re: How to know if there is error when downloading files?

Post by coater »

Thank you very much! :D
coater
Posts: 187
Joined: Sat Sep 29, 2018 1:51 pm

Re: How to know if there is error when downloading files?

Post by coater »

salvadordf wrote: Thu Oct 11, 2018 7:01 am Use the TChromium.OnResourceResponse event and check the content-type header in the response parameter.
If it contains "application/pdf" then you can copy the url from the request.
----------------------------------
procedure TIinformSearchFrm.Chromium1ResourceResponse(Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame;
const request: ICefRequest; const response: ICefResponse;
out Result: Boolean);

begin
if ansipos('application/pdf', response.GetHeader('content-type') ) > 0 then
begin
Chromium1.StopLoad;
Chromium1.StartDownload(request.Url) ;
end;
end;
---------------------------------
If I do that, Many same files will be download.
Is it because of the procedure of ChromiumResourceResponse also been tripped by download?
How to avoid this? (I try the following way and it works. Is there another better way? )
Thank you very much!



------------------------------------
procedure Chromium1BeforeBrowse(Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame;
const request: ICefRequest; user_gesture, isRedirect: Boolean;
out Result: Boolean);
begin
// memo1.Lines.Add(request.Url) ;
if ansipos('.',DownPdfUrl)>0 then // or length(DownPdfUrl)>0
begin
Chromium1.StopLoad;
Chromium1.StartDownload(DownPdfUrl) ;
DownPdfUrl:='';
end;

end;

procedure Chromium1ResourceResponse(Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame;
const request: ICefRequest; const response: ICefResponse;
out Result: Boolean);

begin
memo1.Lines.Add(response.GetHeader('content-type') ) ;
memo1.Lines.Add( browser.MainFrame.Url ) ;
memo1.Lines.Add(request.Url ) ;
memo1.Lines.Add(response.URL ) ;
if ansipos('application/pdf', response.GetHeader('content-type') )>0 then
begin
DownPdfUrl:=request.Url ;
end;
end;

-------------------------------------
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to know if there is error when downloading files?

Post by salvadordf »

coater wrote: Wed Oct 17, 2018 10:10 am Is it because of the procedure of ChromiumResourceResponse also been tripped by download?
How to avoid this? (I try the following way and it works. Is there another better way? )
Use a second TChromium component for the file downloads or keep a copy of the URLs that you are currently downloading to ignore that event if it has the same URL.
coater
Posts: 187
Joined: Sat Sep 29, 2018 1:51 pm

Re: How to know if there is error when downloading files?

Post by coater »

Thank you very much for your kind help!
coater
Posts: 187
Joined: Sat Sep 29, 2018 1:51 pm

Re: How to know if there is error when downloading files?

Post by coater »

Is this a bug?

visit:
epdf


there is no file was downloaded but 'ChromiumBeforeDownload' will be triggered.
Last edited by coater on Wed Oct 17, 2018 1:53 pm, edited 1 time in total.
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to know if there is error when downloading files?

Post by salvadordf »

That website seems to have an embedded PDF viewer.

I wouldn't be surprised if one of the scripts downloads the original PDF to show it and maybe this is what your app is detecting. Perhaps your app is starting a file download when a resource with a PDF content-type is detected.
Post Reply