Javascript 在为';选择文件时检查文件扩展名;文件字段';并使用RubyonRails显示错误消息(如果有)
我正在运行Ruby on Rails 3和Prototype,我想检查为Javascript 在为';选择文件时检查文件扩展名;文件字段';并使用RubyonRails显示错误消息(如果有),javascript,ruby-on-rails,ruby,ajax,prototype,Javascript,Ruby On Rails,Ruby,Ajax,Prototype,我正在运行Ruby on Rails 3和Prototype,我想检查为文件_field选择文件时的文件扩展名,因此如果不是允许的扩展名,我可以显示错误(在我的情况下,只有.doc和.pdf是允许的扩展名) 此时,在我的视图文件中,我通过以下方式查看文件\u字段: page.event.observe('file_field_id', 'change') do |element| # Here I would like to put some code in order to check t
文件_field
选择文件时的文件扩展名,因此如果不是允许的扩展名,我可以显示错误(在我的情况下,只有.doc
和.pdf
是允许的扩展名)
此时,在我的视图文件中,我通过以下方式查看文件\u字段
:
page.event.observe('file_field_id', 'change') do |element|
# Here I would like to put some code in order to check the file extension
# If the file extension isn't '.doc' or '.pdf' I would like to set and show an error text in the 'error_id'
# I know using 'element[:error_id].show' I can show the error, but...
# 1. how to set the error value?
# 2. how to check the file extension?
end
现在我需要检查选择文件后,file\u字段
中的字符串是否具有exention.doc
或.pdf
,如果没有,为错误id设置一个值并显示它
我该怎么做?
更新:我需要使用原型
框架。有可能吗?在JS中,使用正则表达式可以获得文件扩展名
var extens=(/[.]/.exec(myfile)) ? /[^.]+$/.exec(myfile) : undefined;
这里myfile
是文件的地址或路径。这只是取“.”之后的字符串部分。然后,您可以使用进行一些控制检查,如果。比如:
if(extens=="doc" || extens=="pdf")
{
//your method goes here
}
else
{
error_id= ........
}
这一定是可能的。我试过了,但没有成功,否则我就没有发帖!英雄联盟