Rich file upload showing all files on add, even for acceptedTypes=“xls”

烈酒焚心 提交于 2019-12-11 05:08:55

问题


<rich:fileUpload fileUploadListener="# {bean.fileUploadAction}"
 immediateUpload="true" acceptedTypes="xls" maxFilesQuantity="1">        
</rich:fileUpload>

in rich file uplaod i given acceptedTypes="xls" ,but on "add" in the file select box Files of types is showing all. I want to restrict it to only some specific format how can i do that. Any kind of help is appriciated,thanks in advance..


回答1:


This alternative way may solve your problem to some extent. Add an onclick event to your <rich:fileUpload components as below.

<rich:fileUpload onclick="this.getElementsByTagName('input')[0].setAttribute('accept', 'application/vnd.ms-excel');" fileUploadListener="# {bean.fileUploadAction}"
 immediateUpload="true" maxFilesQuantity="1">        
</rich:fileUpload>



回答2:


That's only possible if it generates the following HTML (and the page is opened by a HTML5 compatible webbrowser):

<input type="file" ... accept="application/vnd.ms-excel" />

However, it doesn't do that. Instead, it generates a simple HTML4 compatible <input type="file"> without the accept attribute and does a file extension check on the selected file by JavaScript.

So you're out of luck here. You'd basically need to replace the <rich:fileUpload> by another component which generates exactly the desired HTML with the proper accept attribute set on <input type="file">. As far as I see now, no one JSF component library offers such a component yet. You might need to homegrow it.

See also:

  • HTML5 specification - the accept attribute of type=file


来源:https://stackoverflow.com/questions/14215770/rich-file-upload-showing-all-files-on-add-even-for-acceptedtypes-xls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!