载波图像上传不起作用
Carrierwave image uploading not working
我正在尝试使用通过轨道的carrierwave应用程序将徽标图像上传到Amazon s3存储桶。 但是我的文件上传没有将文件读取为HTTP文件并将NULL添加到数据库中。我无法弄清楚我的代码出了什么问题。
这是我的模型
class StoreLocation < ActiveRecord::Base
mount_uploader :logo_url, ImageUploader
...
end
我的图片上传器
require 'carrierwave/orm/activerecord'
class ImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
#storage :file
storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def cache_dir
# "#{Rails.root}/tmp/uploads"
Rails.root.join 'tmp/uploads'
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg gif png)
end
end
我的store_location/new.html.erb 中的片段
<div>
<input id="picholder" type="file" name = "store_location[logo_url]" style = "width: 0px;">
<img id="storeLogo" src="/assets/Picupload_bg.png" alt="storeLogo" class = "small_thumb"/>
<img src="/assets/brws.png" id="brws_logo_file" class = "brws_btn">
</div>
我的JavaScript store_location.js读取文件。
$(document).on('ready page:load', function () {
jQuery.validator.addMethod("lettersonly", function(value, element) {
return this.optional(element) || /^[A-Za-z ]+$/i.test(value);
}, " ");
function readURLImage(filePath) {
if (filePath.files && filePath.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#storeLogo').attr('src', e.target.result);
}
reader.readAsDataURL(filePath.files[0]);
}
}
$("#picholder").change(function(){
readURLImage(this);
});
$("#brws_logo_file").click(function () {
$("#picholder").trigger('click');
});
});
当我运行它时,我可以在选择任何图像文件时在表单上看到缩略图,但请求中的参数如下:(来自 rails 日志)
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Q0ejEHG2uzPlSlJ2XF8eC7uVXWs76jtEGfXRNCHgyuc=", "store_location"=>{"store_name"=>"Origins", "first_address"=>"Gulberg", "second_address"=>"", "country"=>"Pakistan", "state"=>"Punjab", "city"=>"Lahore", "zip_code"=>"54000", "phone_no"=>"111674446", "logo_url"=>"b3.jpg"}, "commit"=>"Submit"}
INSERT INTO `store_locations` (`city`, `country`, `created_at`, `first_address`, `logo_url`, `phone_no`, `second_address`, `state`, `store_name`, `updated_at`, `user_id`, `zip_code`) VALUES ('Lahore', 'Pakistan', '2014-12-10 10:33:37', 'Gulberg', NULL, '111674446', '', 'Punjab', 'Origins', '2014-12-10 10:33:37', 1, '54000')
我不明白为什么logo_url只是文件名,它应该是这样的
"logo_url"=>#<ActionDispatch::Http::UploadedFile:0x00000009b07c88 @tempfile=#<Tempfile:/tmp/RackMultipart20141209-15502-1q6mzyf>, @original_filename="b3.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name='"store_location[logo_url]'"; filename='"b3.jpg'"'r'nContent-Type: image/jpeg'r'n">
在这方面的任何帮助将不胜感激。谢谢。
默认情况下,Carrierwave有一个column_name
+_url
的方法,用于在某个列中上传远程图像。
例如:
user.logo_url('http://www.example.com/example.png')
将此图像保存在用户模型的logo
列中。
将列从logo_url
重命名为logo
,以便直接上传图像而不启动HTTP请求。
相关文章:
- 使用css()设置背景图像;不起作用
- 图层删除(图像);在 Kinetic.js 中不起作用
- 为什么我的图像幻灯片不起作用
- 为什么这个jQuery图像映射在WordPress上不起作用
- 在页面中使用了两次多个图像上传,但第一个正在工作,另一个不起作用
- 用于图像上传的 ng 文件上传不起作用
- 当图像和链接是 json 对象属性的值时,它们不起作用
- JavaScript 图像加载器不起作用
- 本地存储功能中的图像不起作用
- 使用 nameProp 的 JQuery 图像滚动更新在 Firefox 和 Chrome 中不起作用
- 如果图像具有 guid 作为名称,则 jqZoom 不起作用
- jQuery图像淡入淡出不起作用
- 输入类型的图像在Internet Explorer中不起作用,但在Chrome和Firefox中起作用
- Transloadit-图像自动旋转不起作用
- 滚动过标题后显示图像-尝试不起作用
- 上传前查看图像在IE中不起作用
- 简单的 Java 脚本背景图像不起作用
- 图像预览在Safari中不起作用
- ajax加载图像第二次在弹出jquery上不起作用
- Jquery setTimeout 不起作用(图像幻灯片)