![](/img/trans.png)
[英]Rails 4, submitting a form by ajax and uploading an image by paperclip gives error: InvalidAuthenticityToken
[英]Uploading an image without a form or database in PaperClip
我是Ruby的新手。
我有一個DIV,其中有一個默認圖像。
我要在點擊該圖片或div時上傳另一張圖片。 我不想將此圖像保存到數據庫。 我也不想像大多數回形針教程所說的那樣使用表單(如果有沒有表單的方法)。
您想使用data-urls 。 這是我如何使用它的一個示例:
<div>
<%= f.file_field :image, :id => "image" %>
<output id="image_preview_output">
</output>
</div>
<script>
$(document).ready(function() {
// Check for the various File API support.
if (window.File && window.FileReader && window.FileList && window.Blob) {
} else {
alert('The File APIs are not fully supported in this browser.');
}
});
$('#image').change(function previewImage(evnt) {
var file = evnt.target.files[0];
// Only process image files.
if (!project_featured_image.type.match('image.*')) {
var reader = new FileReader();
reader.onload = (function(theFile) {
return function(e) {
var span = document.createElement('span');
span.innerHTML = ['<img id="image_preview" src="', e.target.result, '" title="', escape(theFile.name), '"/>'].join('');
$('#image_preview_output').html(span);
};
})(file);
// Read in the image file as a data URL.
reader.readAsDataURL(file);
}
});
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.