Javascript + Ruby on Rails: 提交上传文件之前检验文件类型

HTML:

<form class="form-inline" id="upload_form_<%= model %>" action="<%= url_for action: "index" , controller: :admin %>" method="post" enctype="multipart/form-data" οnsubmit="return validateFile('<%= model.to_s %>')">
	<div class="form-group">
		<label for="exampleInputEmail1">File:</label>
		<input type="file" class="form-control" id="input_<%= model %>" name="file_to_upload">
		<input type="hidden" id="input_model" name="input_model" value="<%= model %>" />
		<%= tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token) %>
		<button type="submit" class="btn btn-primary"><i class="fa fa-cloud-upload"></i></button>
	</div>
</form>
JAVAScript:

unction validateFile(model)
{
	//alert("hi");
	//alert(model);
	//return false;
	var file_id = 'input_'+model ; 
	//alert(file_id);
	//return false;
	var path = document.getElementById(file_id).value
	//alert(path);
	var ext = path.split('.').pop().toLowerCase();
	//alert(ext);
	if(ext != "csv" || ext =="") {
		alert('Invalid file type. Please select a CSV file.');
		return false;
	}
	else {
		//alert('right extension!');
		return true;
	}
}

参考链接:

http://stackoverflow.com/questions/651700/how-to-have-jquery-restrict-file-types-on-upload

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值