Page 1 of 1

svg cannot be down by Chromium1.DownloadImage

Posted: Tue May 19, 2020 12:32 pm
by coater
as this:
url:='https://dosrg0qttcg52.cloudfront.net/im ... b2logo.svg';
Chromium1.DownloadImage(url, False, 0, False);

Chromium1DownloadImageFinished :image=nil;

Re: svg cannot be down by Chromium1.DownloadImage

Posted: Tue May 19, 2020 12:48 pm
by salvadordf
Hi,

ICEFImage seems to support PNG, JPEG and BMP formats only.

Use TChromium.StartDownload or use the code in the URLRequest demo to download SVG files.

Re: svg cannot be down by Chromium1.DownloadImage

Posted: Wed May 20, 2020 7:37 am
by coater
Thank you very much!
I want to encode svg or png to base64 without the need to save files.
Is it possible to use a memorystream or another stream to get a svg/png before download?

Re: svg cannot be down by Chromium1.DownloadImage

Posted: Wed May 20, 2020 8:16 am
by salvadordf
The URLRequest demo does exactly what you want.

Re: svg cannot be down by Chromium1.DownloadImage

Posted: Wed May 20, 2020 10:39 am
by coater
I saw the code, but I still have questions
Is it possible to download urls by CEFUrlRequestClientComponent at the same time?
In that code url only can be assigned in DownloadData, so if I create some addrequests, how to deal with urls in DownloadData?

Re: svg cannot be down by Chromium1.DownloadImage

Posted: Thu May 21, 2020 8:19 am
by salvadordf
If I understand you correctly, you want to create several requests to download several URLs.

It would be interesting to use 1 TCEFUrlRequestClientComponent to handle several ICefUrlRequest instances.
In order to do that we would need to have a way to recognize each ICefUrlRequest instance in the TCEFUrlRequestClientComponent.OnDownloadData event.

I tried for a short time long ago and I couldn't find a way to recognize the different ICefUrlRequest instances in created in TCEFUrlRequestClientComponent.OnCreateURLRequest

Calling SomeInterface.SameAs(OtherInterface) didn't work at that time but you can still add a custom HTTP header to the request when you create it and check if that header exists with "request.request.GetHeaderByName" in the TCEFUrlRequestClientComponent.OnDownloadData event.

Re: svg cannot be down by Chromium1.DownloadImage

Posted: Mon May 25, 2020 8:18 am
by coater
The browser receive a message from JS and run the function GetImgBase64Fun. So it is difficult to add a custom HTTP header to the request every time.
So I use TCriticalSection(ImageCS: TCriticalSection) to ensure the CEFUrlRequest download images one by one.




[code]
procedure MyDownImgTobase( aParam : Pointer); stdcall; // : string : PImageRec Parameter
//Function MyDownImgTobase( aParam : Pointer):string;stdcall;
var
// Id: Dword;
aImageRec2: TImageRec;
tempstr, fileexstr: string;
i:integer;
//base64: TBase64Encoding;
m2: TStringStream;
begin
ImageCS.Enter;

IfCEFUrlRequestCreateURLRequest := False;
IfCEFUrlRequestRequestComplete := False;
IfCEFUrlRequestRequestSucess := False;
i:= 0;
aImageRec2.order := TImageRec(aParam^).order ;
aImageRec2.edittype := TImageRec(aParam^).edittype ;
aImageRec2.SpeciName := TImageRec(aParam^).SpeciName ;
aImageRec2.url := TImageRec(aParam^).url; //aParam.url;
aImageRec2.basestr := TImageRec(aParam^).basestr ;
FImgStream := TMemoryStream.Create;
IinformSearchFrm.CEFUrlRequestClientComponent1.AddURLRequest;

repeat
Sleep(500);
i:=i+1;
until (IfCEFUrlRequestRequestComplete or (i>60));

if IfCEFUrlRequestRequestSucess then
begin
try
m2 := TStringStream.Create;
FImgStream.Position := 0;
tempstr := BaseImage(FImgStream);
fileexstr:= '';
if (ansipos('.',aImageRec2.url)>0) then
begin
fileexstr:= RightStr (aImageRec2.url, Length(aImageRec2.url) - LastPos('/',aImageRec2.url));
if (ansipos('.',fileexstr)>0) then
fileexstr := RightStr (fileexstr, Length(fileexstr) - LastPos('.', fileexstr));
end;
if fileexstr = '' then fileexstr := CheckImgType(FImgStream);
finally
//base64.Free;
m2.Free;
FImgStream.Free;
end;
tempstr:= aImageRec2.SpeciName + ' = ' + DealJsSpeChar('data:image/'+ fileexstr + ';base64,'+tempstr) ;
TFrm.Chromium1.Browser.MainFrame.ExecuteJavaScript(tempstr, TFrm.Chromium1.Browser.MainFrame.Url, 0);
end;

ImageCS.Leave;

end;

function TFrm.GetImgBase64Fun( aurl: TImageRec ): Boolean;
var //aImageUB :ImageUB;
Id: Dword;
aHandle : THandle;
//aImageRec: TImageRec;
begin
Result := false;
Id:=0;
aHandle := 0;
aHandle := CreateThread(nil, 0, @MyDownImgTobase, @aImageRec, 0, Id ); // BeginThread
if aHandle>0 then
Result:=true;
end;
[/code]

Re: svg cannot be down by Chromium1.DownloadImage

Posted: Fri Jul 17, 2020 6:18 am
by coater
CEFUrlRequestClientComponent1 should be released or the quest should be released after download and before close?

Re: svg cannot be down by Chromium1.DownloadImage

Posted: Sat Jul 18, 2020 2:16 pm
by salvadordf
All downloads should stop before closing the application.

It's not necessary to destroy TCEFUrlRequestClientComponent manually.

If your application keeps copies of the requests then set them to nil before closing the application.

Re: svg cannot be down by Chromium1.DownloadImage

Posted: Sun Jul 19, 2020 1:35 pm
by coater
I got it, thank you very much!