【发布时间】:2015-03-15 16:55:45
【问题描述】:
我有一个马模型和一个照片模型。由于我使用的是 Heroku,因此我正在使用 jQuery File Upload 来调整(客户端)图像的大小并直接保存在 Amazon s3 上。
我见过其他类似的问题,使用载波、回形针或非常古老的问题。我不确定你为什么要使用carrierwave/paperclip,但我认为根据heroku所说的,我不希望图像访问服务器可能导致超时。
Heroku recommends 使用 jQuery File Upload 并显示 js 将新文件输入附加到图像链接的值(从 amazon s3 返回)。单独保存照片时我有这个工作。我现在想让它以嵌套形式为 Horse 工作,但 js 没有找到输入(因为它还不存在,因为我认为它是嵌套的)。
我将 Cocoon 用于嵌套表单(我愿意接受任何更好的方法)。我对 javascript/jQuery 不太熟悉,但据我所知,Cocoon 会“隐藏”嵌套元素,直到我单击通过 add_association 添加它。
haml查看代码:
= link_to_add_association 'add photo', f, :photos
点击“添加照片”之前的html源代码
<div class='links'>
<a class="btn btn-default btn-sm add_fields" data-association-insertion-method="after" data-association="photo" data-associations="photos" data-association-insertion-template="<div class='nested-fields'>
<fieldset class="inputs"><ol><input type="file" name="horse[photos_attributes][new_photos][url]" id="horse_photos_attributes_new_photos_url" />
<input type="hidden" name="horse[photos_attributes][new_photos][_destroy]" id="horse_photos_attributes_new_photos__destroy" value="false" /><a class="remove_fields dynamic" href="#">remove photo</a>
</ol></fieldset>
</div>
" href="#">add photo</a>
如何使用此输入以及如何处理正确添加的多个文件上传?
我目前上传的js:
$(function() {
if ($('#new_horse').length > 0) {
$.get( "/presign", function( s3params ) {
$('.direct-upload').find("input:file").each(function(i, elem) {
var fileInput = $(elem);
var form = $(fileInput.parents('form:first'));
var submitButton = form.find('input[type="submit"]');
var progressBar = $("<div class='bar'></div>");
var barContainer = $("<div class='progress'></div>").append(progressBar);
fileInput.fileupload({
fileInput: fileInput,
url: "http://" + s3params.url.host,
type: 'POST',
autoUpload: true,
formData: s3params.fields,
paramName: 'file', // S3 does not like nested name fields i.e. name="user[avatar_url]"
dataType: 'XML', // S3 returns XML if success_action_status is set to 201
disableImageResize: false,
imageQuality: 0.5,
disableImageResize: /Android(?!.*Chrome)|Opera/
.test(window.navigator && navigator.userAgent),
imageMaxWidth: 500,
imageMaxHeight: 1000,
imageOrientation: true, //auto rotates images
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i, //I added this to jquery.fileupload-validate: alert('Must Be JPG GIF or PNG Image')
replaceFileInput: false,
progressall: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
progressBar.css('width', progress + '%')
},
start: function (e) {
submitButton.prop('disabled', true);
fileInput.after(barContainer);
progressBar.
css('background', 'green').
css('display', 'block').
css('width', '0%').
text("Loading...");
},
done: function(e, data) {
submitButton.prop('disabled', false);
progressBar.text("Pre-uploading done... Please Save or Cancel");
// extract key and generate URL from response
var key = $(data.jqXHR.responseXML).find("Key").text();
var url = 'https://' + s3params.url.host +'/' + key;
// remove first input to prevent phantom upload delay
fileInput.remove();
// create new hidden input with image url
var input = $("<input />", { type:'hidden', name: fileInput.attr('name'), value: url })
var imgPreview = '<img src="' + url + '">';
form.append(input);
form.append(imgPreview);
},
fail: function(e, data) {
submitButton.prop('disabled', false);
progressBar.
css("background", "red").
text("Failed");
}
});
});
}, 'json');
}
});
【问题讨论】:
标签: javascript jquery ruby-on-rails