rails mysql mongoid carrierware gridfs 实现图片上传功能
2015-05-15 15:06
537 查看
1.安装
Git: git的安装MongoDB: MongoDB安装
Nginx: Nginx安装
注: 官方的nginx没有nginx-gridfs功能,需要手动编译进去
安装步骤:
[bash]
cd /tmp
git clone https://github.com/mdirolf/nginx-gridfs
cd nginx-gridfs/
git submodule init
git submodule update
#在 gcc 版本 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC) 上编译时,可能会遇到问题。可以在configure时加上
./configure –add-module=/path/to/nginx-gridfs/source/ –with-cc-opt=-Wno-missing-field-initializers
make && make install
#编辑nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
#注: gridfs 指路由名
location /gridfs/ {
gridfs my_app
root_collection=pics
field=_id
type=int
user=foo
pass=bar;
mongo 127.0.0.1:27017;
}
[/bash]
参考: https://github.com/mdirolf/nginx-gridfs http://serverfault.com/questions/345516/compiling-nginx-with-nginx-gridfs-getting-mongo-c-driver-errors-during-make
二.rails配置
1,搭建rails环境 -> rvm 参考:rails环境搭建[bash]
vim Gemfile
source ‘http://rubygems.org’
gem ‘rails’
gem ‘mysql2′
gem ‘carrierwave’
gem ‘mini_magick’
gem ‘mongo’
gem ‘bson’
gem ‘bson_ext’
bundle install
[/bash]
2,创建一个rails项目
[bash]
rails new product –database=mysql
rails g scaffold photo name:string image:string #image存储图片名
[/bash]
3,通过generator生成uploader 文件在app/uploader/base_uploader.rb
[bash]
rails g uploader base
#以上创建的父类文件,可以在app/uploader下创建子类文件用于不同的图片上传需要
#修改base_uploader.rb文件并加入MiniMagic,用来对图片进行处理,改变大小等。
# encoding: utf-8
class BaseUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :grid_fs
end
vim app/uploaders/photo_uploader.rb
# encoding: utf-8
class PhotoUploader < BaseUploader
process :resize_to_limit => [800, 800]
version :thumb do
process :resize_to_limit => [30, 30]
end
end
[/bash]
4, 在model中 mount uploader到image 字段中
[bash]
class Photo < ActiveRecord::Base
attr_accessible :image, :name
mount_uploader :image, PhotoUploader
end
[/bash]
5.在config/initializers/下创建文件carrierwave.rb配置mongodb
[bash]
vim config/initializers/carrierwave.rb
CarrierWave.configure do |config|
config.grid_fs_database = ‘photos’ #数据库名
config.grid_fs_host = ‘localhost’ #mongodb地址
config.grid_fs_access_url = "/uploader" #好像是访问时图片url的前缀
end
[/bash]
6.html模板部分
[bash]
#form
<div class="field">
<%= f.label :image %><br />
<%= f.file_field :image %>
</div>
#index
<%= image_tag photo.image %>
<%= image_tag photo.image_url(:thumb) %>
[/bash]
7. 启动部分
如果是在develop模式下,需要配置路由已经创建controller文件
[bash]
vim app/controller/gridfs_controller.rb
class GridfsController < ActionController::Metal
def serve
gridfs_path = env["PATH_INFO"].gsub("/upload/", "")
begin
gridfs_file = Mongo::GridFileSystem.new(Mongo::DB.new(‘photos’, Mongo::Connection.new(‘localhost’))).open(gridfs_path, ‘r’)
self.response_body = gridfs_file.read
self.content_type = gridfs_file.content_type
rescue Exception => e
self.status = :file_not_found
self.content_type = ‘text/plain’
self.response_body = ”
raise e
end
end
end
vim config/routes.rb
if Rails.env.development?
match "/upload/*path" => "gridfs#serve"
end
[/bash]
8.配置nginx
[bash]
location /images/ {
gridfs photos root_collection=fs field=filename type=string;
mongo 127.0.0.1:27017;
}
[/bash]
9. 安装 nginx-gridfs 模块
注意事项: 如果需要使用nginx直接读取mongo gridfs, 需要重新编辑nginx 安装nginx-gridfs模块官方的nginx没有nginx-gridfs功能,因此需要手动编译进去。
安装步骤见项目的github主页: https://github.com/mdirolf/nginx-gridfs
遇到的问题及解决方法:
[bash]
# 在 gcc 版本 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC) 上编译时,可能会遇到问题。可以在configure时加上
–with-cc-opt=-Wno-missing-field-initializers
# 选项试试。( 见 http://serverfault.com/questions/345516/compiling-nginx-with-nginx-gridfs-getting-mongo-c-driver-errors-during-make )
# 当make是出现错误
#错误: 变量‘chunksize’被设定但未被使用 [-Werror=unused-but-set-variable]
#cc1: all warnings being treated as errors
#去掉objs/makefile中 -Werror这样一段,重新make就ok了
[/bash]
相关文章推荐
- RubyOnRails在redactor中通过paperclip实现图片上传并编辑的功能
- rails使用bootstrap3-wysiwyg可视化编辑器并实现自定义图片上传插入功能
- PHP使用curl请求实现post方式上传图片文件功能示例
- Summernote实现图片上传功能的简单方法
- ueditor1_4_3-utf8-jsp 配置实现上传图片的功能遇到的问题,改变图片上传请求地址
- 完成一个个人博客,博客头像可上传本地图片;部分图片实现点击看大图功能
- jsp中点击图片弹出文件上传界面及预览功能的实现
- PHP+KINDEDIT+mysql+javascript实现文本编辑和文件上传功能实例
- web编程3--实现图片上传功能
- node+layui实现图片上传预览和删除功能
- 微信小程序实现图片上传功能实例(前端+PHP后端)
- 在asp.Net中使用fileuplod控件实现上传图片的功能
- Android实现图片选择上传功能实例
- SpringMVC+easyUI实现上传图片功能小例子
- 基于ASP.NET+easyUI框架实现图片上传功能(表单)
- (转)淘淘商城系列——实现图片上传功能
- java使用CKEditor实现图片上传功能
- Servlet+Jsp实现图片或文件的上传功能具体思路及代码
- SmartUpload实现图片上传&带预览功能
- ueditor1_4_3-utf8-jsp 配置实现上传图片的功能