Welcome Guest Search | Active Topics | Sign In | Register

Getting Page Documents and Limiting the Download Size Options
Red Olive
Posted: Tuesday, February 18, 2020 7:21:52 AM
Rank: Member
Groups: Member

Joined: 2/18/2020
Posts: 10
Hello

While loading a page, it may contain fonts, images, videos and so on.
How to handle downloading such documents?

First I look for download events of WebView. But no success. Those images and fonts will not pass to download event. So where to look for them?

and How to limit the download size? for example, I need to get only 1MB of a 10MB image.
eo_support
Posted: Tuesday, February 18, 2020 9:31:10 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,071
Hi,

The download event is not what you are looking for. Those events are triggered when explicitly downloads a file. A resource can either be "loaded" or "downloaded". If a resource is to be "loaded", it is fetched from the server and passed to the corresponding parser (for example, a HTML file would be passed to the HTML parser), which would then parse and display it in the browser window. If a resource is to be downloaded, it is fetched from the server and then saved to a temp file, no parsers are involved.

There is no built-in features for you to limit download size of dependency resources such as images. If you must control that, you will have to take over the loading process yourself with a custom resource handler:

https://www.essentialobjects.com/doc/webbrowser/advanced/resource_handler.aspx

This allows you to selectively intercept certain resources and handle actually resource fetching yourself. For example, you could use a custom resource handler to intercept all font file request and then load the font file from your local database and then pass it to the browser. The browser will still think it got the font file from the server. You can implement whatever logic you wish in this part. For example, if you have intercepted an image request, you can send the request to the server, and as soon as you get the response header that tells you the size of the image, you can check the size and decide whether you wish to continue. If it it too big, you can simply return from your custom resource handler and the browser engine will get an "corrupted" image.

Because the loader layer of the browser engine is highly optimized, it is not a good idea to completely take over the entire loader layer with your own code. As such you should only intercept those requests that you are interested and leave the rest for the browser engine to handle.

Hope this helps. Please feel free to let us know if you still have any questions.

Thanks!

Red Olive
Posted: Saturday, February 22, 2020 3:15:03 AM
Rank: Member
Groups: Member

Joined: 2/18/2020
Posts: 10
Quote:
public override bool Match(ResourceHandlerContext context)
{
if (context.Request.ResourceType == ResourceType.Image)
return false;
else
return true;
}


I used this code to remove images from page response, but I think it will reject the whole request. so how really handle such replacement? replace images with nothing or the one I need.

Even If I use to like this:

Quote:
public override bool Match(ResourceHandlerContext context)
{
return true;
}


nothing will happen and the response will be empty, but I have set true for everything that will happen.
eo_support
Posted: Monday, February 24, 2020 9:29:28 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,071
Hi,

You need to override both Match and ProcessRequest. These two works together. If you return true from Match, that means you WANT to handle this request yourself. In that case you must override ProcessRequest to actually handle this request. So you can't just say you want to handle it (by returning true from Match) but then do not actually handle it (by not overriding ProcessRequest).

You can take a look of the custom resource handler sample code in TabbedBrowser sample application to get an idea how this works.

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.