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.

onFileDialog - acceptFilters parameter

Post Reply
michal@dorfin.waw.pl
Posts: 41
Joined: Sun Feb 05, 2017 8:53 am

onFileDialog - acceptFilters parameter

Post by michal@dorfin.waw.pl »

I'm using onfileDialog event in TChromium. It works fine.
But I noticed that "acceptFilters" doesn't contain proper list of accepted extentions.
When I open the default "open file" dialog with:
Result:=FALSE;
I can see in open file dialog the list of accepted extentions is different.
Here is the comboBox in open file dialog (default Chrome).
Zrzut ekranu 2021-11-28 o 12.11.18.png
acceptFiles (when I extract strings from it) looks like this:
image/jpeg
application/pdf
.jpeg
.jpg
.pdf

This is huge difference. How to obtain proper accept files list ?
You do not have the required permissions to view the files attached to this post.
User avatar
salvadordf
Posts: 4580
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: onFileDialog - acceptFilters parameter

Post by salvadordf »

Hi,

Chromium only gives content types and extensions in "acceptFilters". These are the code comments for that event :

Code: Select all

  ///
  // Called to run a file chooser dialog. |mode| represents the type of dialog
  // to display. |title| to the title to be used for the dialog and may be NULL
  // to show the default title ("Open" or "Save" depending on the mode).
  // |default_file_path| is the path with optional directory and/or file name
  // component that should be initially selected in the dialog. |accept_filters|
  // are used to restrict the selectable file types and may any combination of
  // (a) valid lower-cased MIME types (e.g. "text/*" or "image/*"), (b)
  // individual file extensions (e.g. ".txt" or ".png"), or (c) combined
  // description and file extension delimited using "|" and ";" (e.g. "Image
  // Types|.png;.gif;.jpg"). |selected_accept_filter| is the 0-based index of
  // the filter that should be selected by default. To display a custom dialog
  // return true (1) and execute |callback| either inline or at a later time. To
  // display the default dialog return false (0).
  ///
You need to create the file filter list based on the information in "acceptFilters".

If you only have an extension and you need the mime type you can call CefGetMimeType.
If you only have a mime type and you want the list of extensions then you can call CefGetExtensionsForMimeType.
CefGetMimeType and CefGetExtensionsForMimeType are declared in uCEFMiscFunctions.pas.

The Windows registry also has the information about all the registered file extensions, their content type and the perceived type in HKEY_CLASSES_ROOT.

Additionally, you can use the information from the online lists of mime types :
https://www.digipres.org/formats/mime-types/
https://codingislove.com/list-mime-types-2016/
https://docs.w3cub.com/http/basics_of_http/mime_types/complete_list_of_mime_types

Then you can use SHGetFileInfo to get the file type description :
https://stackoverflow.com/questions/3780028/how-can-i-get-the-description-of-a-file-extension-in-net
https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shgetfileinfow
https://docs.microsoft.com/en-us/windows/win32/api/shellapi/ns-shellapi-shfileinfoa
michal@dorfin.waw.pl
Posts: 41
Joined: Sun Feb 05, 2017 8:53 am

Re: onFileDialog - acceptFilters parameter

Post by michal@dorfin.waw.pl »

Dear salvadordf,

I don't have any problem neither with file extensions nor mime types.
I showed You that default open file dialog has many different options in comboBox (including the names of the filter).
These options depend on the web page calling function "Upload file".
So I thought that "accept_filters" parameter should have the same list of accepted files' extensions that is in this comboBox.
As You can see on the screenshot there are many extensions allowed in comboBox but the "acceptFilters" has only three: pdf / jpg / jpeg.
Do You have any idea why there is so much difference ?
User avatar
salvadordf
Posts: 4580
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: onFileDialog - acceptFilters parameter

Post by salvadordf »

The accept_filters parameter also has two mime types image/jpeg and application/pdf.
Chrome gets the file extensions of those mime types and tries to get the file type descriptions to replace those entries.
Additionally it also adds *.* at the end.

This is not the first time someone asks about this feature so I'll create a small helper class and use it in the MiniBrowser demo.
User avatar
salvadordf
Posts: 4580
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: onFileDialog - acceptFilters parameter

Post by salvadordf »

I just uploaded a new version of CEF4Delphi with a new TCEFFileDialogInfo class.

Check the MiniBrowser VCL demo to see it working.
michal@dorfin.waw.pl
Posts: 41
Joined: Sun Feb 05, 2017 8:53 am

Re: onFileDialog - acceptFilters parameter

Post by michal@dorfin.waw.pl »

Thank You. It was very helpful.
Now one can correct the default Chrome behaviour (including removing duplicated extensions) 😉.
Post Reply