adding filter to file upload in gwt

2,001 views
Skip to first unread message

aman

unread,
Jul 6, 2011, 2:18:27 AM7/6/11
to Google Web Toolkit
I am using the basic FileUpload control in gwt and wanted to add the
filter on the extensions allowed in the browsing window, which
restrict the user to select only specific extensions (like xls,xlsx
etc). Tell me how I can achieve this as I want to restrict on the
browsing window itself and cant wait for the Button submit event.

Thanks

Amandeep

Jim Douglas

unread,
Jul 6, 2011, 7:40:21 PM7/6/11
to Google Web Toolkit
Untested, and no guarantees, but try something like this:

String mimeList = "application/vnd.ms-excel,application/
msexcel,application/x-msexcel,application/x-ms-excel,application/
vnd.ms-excel,application/x-excel,application/x-
dos_ms_excel,application/xls,application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet";

myFileUploadWidget.getElement().setPropertyString("accept", mimeList);

Reference:

http://www.w3.org/TR/html-markup/input.file.html

aman

unread,
Jul 7, 2011, 1:06:38 AM7/7/11
to Google Web Toolkit
Hi,
Thanks for your help it really worked,but it is also giving me the
option in the browsing window to select "All files" can I remove this
option?


Thanks,
Amandeep

On Jul 7, 4:40 am, Jim Douglas <jdou...@basis.com> wrote:
> Untested, and no guarantees, but try something like this:
>
> String mimeList = "application/vnd.ms-excel,application/
> msexcel,application/x-msexcel,application/x-ms-excel,application/
> vnd.ms-excel,application/x-excel,application/x-
> dos_ms_excel,application/xls,application/vnd.openxmlformats-
> officedocument.spreadsheetml.sheet";
>
> myFileUploadWidget.getElement().setPropertyString("accept", mimeList);
>
> Reference:
>
> http://www.w3.org/TR/html-markup/input.file.html
>
> On Jul 5, 11:18 pm, aman <bhatia.ama...@gmail.com> wrote:
>
>
>
>
>
>
>
> > I am using the basic FileUpload control ingwtand wanted to add the
> >filteron the extensions allowed in the browsing window, which

Jim Douglas

unread,
Jul 7, 2011, 1:39:34 AM7/7/11
to Google Web Toolkit
I'm actually surprised that it worked; what browser, and how did it
represent that setting to the user?

Keep in mind that there's no guarantee that this will work in all
browsers; the W3C spec says only that this "provides the user agent
with a hint of file types to accept". How it does that is up to the
browser, and the browser is free to give the user the option to select
any file, notwithstanding your list of acceptable MIME types. So
you're still going to want to validate the file when the user clicks
the submit button.

http://www.w3.org/TR/html-markup/input.file.html

ⓘ accept = list of MIME types
Provides the UA with a hint of what file types the server is able to
accept.
A set of comma-separated strings, each of which is a valid MIME type,
with no parameters.

aman

unread,
Jul 7, 2011, 1:56:31 AM7/7/11
to Google Web Toolkit, jdo...@basis.com
I did not tested it on other browsers initially, now when I am running
it on IE it is not working it works well with google chrome with the
option of "Custom files" and "All files" in the browsing window.I
wanted to remove the "All files" option but now it seems that it is
browser dependent so I am looking for some other options.Please
suggest me some other way which do not bring browser dependency in
picture.

On Jul 7, 10:39 am, Jim Douglas <jdou...@basis.com> wrote:
> I'm actually surprised that it worked; what browser, and how did it
> represent that setting to the user?
>
> Keep in mind that there's no guarantee that this will work in all
> browsers; the W3C spec says only that this "provides the user agent
> with a hint offiletypes to accept".  How it does that is up to the
> browser, and the browser is free to give the user the option to select
> anyfile, notwithstanding your list of acceptable MIME types.  So
> you're still going to want to validate thefilewhen the user clicks
> the submit button.
>
> http://www.w3.org/TR/html-markup/input.file.html
>
> ⓘ accept = list of MIME types
> Provides the UA with a hint of whatfiletypes the server is able to

Jim Douglas

unread,
Jul 7, 2011, 2:16:02 AM7/7/11
to Google Web Toolkit
Aman,

Please respond only on the list, not via private email.

There are no other options. Assume that the browser is free to ignore
the accept attribute, and validate the input when the user clicks the
submit button.

http://www.w3schools.com/TAGS/att_input_accept.asp

http://stackoverflow.com/questions/71944/how-do-i-validate-the-file-type-of-a-file-upload

aman

unread,
Jul 7, 2011, 2:31:58 AM7/7/11
to Google Web Toolkit
Jim,

Thanks for your help.

Aman
Reply all
Reply to author
Forward
0 new messages