jquery上载文件错误消息windows
我正在改变/改进,现在我一直停留在错误消息上 我在中发现了一些错误变量,我想知道如何将它们与验证连接起来,以便在文件太大或太小时显示一个消息窗口 如果有人帮助我,我将不胜感激 这是index.html.erb的小分界点:jquery上载文件错误消息windows,jquery,ruby-on-rails,jquery-validate,Jquery,Ruby On Rails,Jquery Validate,我正在改变/改进,现在我一直停留在错误消息上 我在中发现了一些错误变量,我想知道如何将它们与验证连接起来,以便在文件太大或太小时显示一个消息窗口 如果有人帮助我,我将不胜感激 这是index.html.erb的小分界点: <script> var fileUploadErrors = { maxFileSize: 'File is too big', minFileSize: 'File is too small', acceptFileTypes: 'Filetyp
<script>
var fileUploadErrors = {
maxFileSize: 'File is too big',
minFileSize: 'File is too small',
acceptFileTypes: 'Filetype not allowed',
maxNumberOfFiles: 'Max number of files exceeded',
uploadedBytes: 'Uploaded bytes exceed file size',
emptyResult: 'Empty file upload result'
};
</script>
<!-- The template to display files available for upload -->
<script id="template-upload" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-upload fade">
<td class="preview"><span class="fade"></span></td>
<td class="name"><span>{%=file.name%}</span></td>
<td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
{% if (file.error) { %}
<td class="error" colspan="2"><span class="label label-important">{%=locale.fileupload.error%}</span> {%=locale.fileupload.errors[file.error] || file.error%}</td>
{% } else if (o.files.valid && !i) { %}
<td class="start">{% if (!o.options.autoUpload) { %}
<button class="btn btn-mini btn-primary">
<i class="icon-upload icon-white"></i>
<span>{%=locale.fileupload.start%}</span>
</button>
{% } %}</td>
{% } else { %}
<td colspan="2"></td>
{% } %}
<td class="cancel">{% if (!i) { %}
<button class="btn btn-mini btn-warning">
<i class="icon-ban-circle icon-white"></i>
<span>{%=locale.fileupload.cancel%}</span>
</button>
{% } %}</td>
</tr>
{% } %}
</script>
<!-- The template to display files available for download -->
<script id="template-download" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-download fade">
{% if (file.error) { %}
<td></td>
<td class="name"><span>{%=file.name%}</span></td>
<td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
<td class="error" colspan="2"><span class="label label-important">{%=locale.fileupload.error%}</span> {%=locale.fileupload.errors[file.error] || file.error%}</td>
{% } else { %}
<td class="preview">{% if (file.thumbnail_url) { %}
<a href="{%=file.url%}" title="{%=file.name%}" rel="gallery" download="{%=file.name%}"><img src="{%=file.thumbnail_url%}"></a>
{% } %}</td>
<td class="name">
<a href="{%=file.url%}" title="{%=file.name%}" rel="{%=file.thumbnail_url&&'gallery'%}" download="{%=file.name%}">{%=file.name%}</a>
</td>
<td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
<td colspan="2"></td>
{% } %}
<td class="delete">
<button class="btn btn-mini btn-danger" data-type="{%=file.delete_type%}" data-url="{%=file.delete_url%}">
<i class="icon-trash icon-white"></i>
<span>{%=locale.fileupload.destroy%}</span>
</button>
<input type="checkbox" name="delete" value="1">
</td>
</tr>
{% } %}
</script>
这可能有助于您进行一些与文件相关的验证:
class Upload < ActiveRecord::Base
attr_accessible :upload, :upload_file_name, :upload_file_size
has_attached_file :upload
include Rails.application.routes.url_helpers
validates :upload_file_name, :presence => true,
#:uniqueness =>{:case_sensitive => false},
:format =>{:with => %r{\.(cel)$}i}
# validates_presence_of :upload_file_name
validates_uniqueness_of :upload_file_name, :message => "blabla"
#validates_attachment_size :upload, :in =>1.megabytes..20.megabytes
validates :upload_file_size, :inclusion => {:in =>10.megabytes..20.megabytes}
def to_jq_upload
{
"name" => read_attribute(:upload_file_name),
"size" => read_attribute(:upload_file_size),
"url" => upload.url(:original),
"delete_url" => upload_path(self),
"delete_type" => "DELETE"
}
end
end
在index.html.erb
中找到此$(“#fileupload”).fileupload
,并更改为类似以下内容:
$('#fileupload').fileupload({
maxFileSize: 100000000,
acceptFileTypes: '/^image\/(gif|jpeg|png)$/',
previewSourceFileTypes: '/^image\/(gif|jpeg|png)$/',
});
您应该查看“blueimp/jQuery文件上载”wiki页面以了解更多错误类型。这可能有助于您进行一些与文件相关的验证:
class Upload < ActiveRecord::Base
attr_accessible :upload, :upload_file_name, :upload_file_size
has_attached_file :upload
include Rails.application.routes.url_helpers
validates :upload_file_name, :presence => true,
#:uniqueness =>{:case_sensitive => false},
:format =>{:with => %r{\.(cel)$}i}
# validates_presence_of :upload_file_name
validates_uniqueness_of :upload_file_name, :message => "blabla"
#validates_attachment_size :upload, :in =>1.megabytes..20.megabytes
validates :upload_file_size, :inclusion => {:in =>10.megabytes..20.megabytes}
def to_jq_upload
{
"name" => read_attribute(:upload_file_name),
"size" => read_attribute(:upload_file_size),
"url" => upload.url(:original),
"delete_url" => upload_path(self),
"delete_type" => "DELETE"
}
end
end
在index.html.erb
中找到此$(“#fileupload”).fileupload
,并更改为类似以下内容:
$('#fileupload').fileupload({
maxFileSize: 100000000,
acceptFileTypes: '/^image\/(gif|jpeg|png)$/',
previewSourceFileTypes: '/^image\/(gif|jpeg|png)$/',
});
您应该查看“blueimp/jQuery文件上载”wiki页面以了解更多错误类型