这是我的代码
的application.js
$(document).ready(function(){
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'
};
$('#fileupload').fileupload({
autoUpload : true,
maxRetries : 100,
retryTimeout : 500,
fail : function(e, data) {
var fu = $(this).data('blueimp-fileupload') || $(this).data('fileupload'), retries = data.context.data('retries') || 0, retry = function() {
$.getJSON('#fileupload', {
file : data.files[0].name
}).done(function(result) {
var file = result.file;
data.uploadedBytes = file && file.size;
data.data = null;
data.submit();
}).fail(function() {
fu._trigger('fail', e, data);
});
};
if (data.errorThrown !== 'abort' && data.uploadedBytes < data.files[0].size && retries < fu.options.maxRetries) {
retries += 1;
data.context.data('retries', retries);
window.setTimeout(retry, retries * fu.options.retryTimeout);
return;
}
data.context.removeData('retries');
$.blueimp.fileupload.prototype.options.fail.call(this, e, data);
}
});
$.getJSON($('#fileupload').prop('action'), function (files) {
var fu = $('#fileupload').data('blueimpFileupload'),
template;
fu._adjustMaxNumberOfFiles(-files.length);
console.log(files);
template = fu._renderDownload(files)
.appendTo($('#fileupload .files'));
fu._reflow = fu._transition && template.length &&
template[0].offsetWidth;
template.addClass('in');
$('#loading').remove();
});
控制器
def index
@assets = Asset.all
respond_to do |format|
format.html
format.json { render json: @assets.map{|asset| asset.to_jq_asset } }
end
end
模型
class Asset < ActiveRecord::Base
has_attached_file :upload
do_not_validate_attachment_file_type :upload
include Rails.application.routes.url_helpers
def to_jq_asset
{
"id" => read_attribute(:id),
"name" => read_attribute(:upload_file_name),
"size" => read_attribute(:upload_file_size),
"content_type" => read_attribute(:upload_content_type),
"url" => upload.url(:original),
"delete_url" => asset_path(self),
"delete_type" => "DELETE"
}
end
end
为此,我使用了rails 4,ruby 2.1.0和文件上传使用了paperclip和jquery-fileupload-rails gem 提前谢谢。
答案 0 :(得分:1)