rails应用使用carrierwave和mini_magick上传用户头像
1. 在Gemfile添加
gem 'carrierwave'
gem 'mini_magick'
执行 bundle install
2. 生成uploader
rails generate uploader UserPic
生成文件uploaders/user_pic_uploader.rb ,修改如下
# 修改文件存储位置
def store_dir
# "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
"user_head_pic"
end # 设置默认头像
def default_url(*args)
"/assets/user_head.png"
end # 修改文件名
def filename
if original_filename
@name ||= Digest::MD5.hexdigest(current_path)
"#{@name}#{file.basename}.#{file.extension}"
end
end
3. 给user表添加用户头像字段
添加migrate文件并执行 rails db:migrate
class AddHeadPicToUsers < ActiveRecord::Migration[5.1]
def change
add_column :users,:head_pic,:string,:comment=>'头像'
end
end
4. 修改app/models/user.rb
user.rb中添加UserPicUploader
class User < ActiveRecord::Base
mount_uploader :head_pic, UserPicUploader
end
5. 保存头像
cotroller中对应的action如下
def update_user_pic
image_info = params[:avatar]||""
if image_info.include? "data:image/png"
png = Base64.decode64(image_info['data:image/png;base64,'.length .. -1])# 将 Base64 编码的参数用 Base64 解码,得到数据的二进制表示,也就是图片本身的二进制数据
unless Dir.exists?("#{Rails.root}/public/user_head_pic")
FileUtils.mkdir_p("#{Rails.root}/public/user_head_pic")
end
file_name = "img_#{@current_user.user_no}.png"# 根据当前用户工号声明一个文件名
file_path = "#{Rails.root}/public/user_head_pic/#{file_name}" # 确定文件保存路径
File.open(file_path,'wb') do |item|# 根据文件路径创建新文件
item.write(png)# 将前面的二进制数据写到文件里
end
@current_user.head_pic = File.open(Rails.root+"public/user_head_pic/#{file_name}")
@current_user.save
flash[:notice] = "保存成功!"
else
flash[:notice] = "保存失败!"
end redirect_to :action=>:update_head
end
6. 显示头像
<img src="<%= @user.head_pic.url%>" />
rails应用使用carrierwave和mini_magick上传用户头像的更多相关文章
- 【NopCommerce 3.1】asp.net mvc 利用jQuery from.js上传用户头像
纯代码不解释. 在CusotmerControllers中添加上传方法 /// <summary> /// ajax上传用户头像 /// </summary> /// < ...
- rails中使用CarrierWave实现文件上传的功能
之前在用django写blog的时候头像上传和头像预览都是使用原生的js实现的,之前也有写了一篇blog.好了开始进入正题 rails中实现头像上传十分的方便,只要通过CarrierWave这个gem ...
- 手机端用来上传用户头像的代码canvas
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Vue+axios+Node+express实现文件上传(用户头像上传)
Vue 页面的代码 <label for='my_file' class="theme-color"> <mu-icon left value="bac ...
- 微信小程序--更换用户头像/上传用户头像/更新用户头像
changeAvatar:function (){ var that=this; wx.chooseImage({ count: 1, // 默认9 sizeType: ['original', 'c ...
- 升级IOS8游戏上传自定义头像功能失效的问题
为了支持arm64,之前已经折腾了很久,昨晚打包准备提交苹果审核时,测试那边的同事反馈说游戏上传自定义头像功能不可用了. 游戏上传自定义功能的简介:卡牌游戏最初是<比武招亲>中有一个充VI ...
- Android图片上传(头像裁切+原图原样)
下面简单铺一下代码: (一)头像裁切.上传服务器(代码) 这里上边的按钮是头像的点击事件,弹出底部的头像选择框,下边的按钮跳到下个页面,进行原图上传. ? 1 2 3 4 5 6 7 8 9 10 1 ...
- cocoon + carrierwave 多图片上传用法
gem 'cocoon' gem 'carrierwave' gem 'mini_magick' 1.图片上传carrierwave配置,github 自己手动添加的配置,没用命令生成 在app下新建 ...
- 文件上传之——用SWF插件实现文件异步上传和头像截取
之前写过几篇文件上传,那些都不错.今天小编带领大家体会一种新的上传方法,及使用Flash插件实现文件上传. 使用Flash的好处就是可以解决浏览器兼容性问题.之前我写的一个快捷复制功能也是利用的Fla ...
随机推荐
- UIButton中的**EdgeInsets是做什么用的?
UIButton中的**EdgeInsets是做什么用的? UIEdgeInsetsMake Creates an edge inset for a button or view.An inset i ...
- 使用UIScreenEdgePanGestureRecognizer写iOS7侧边栏
使用UIScreenEdgePanGestureRecognizer写iOS7侧边栏 A UIScreenEdgePanGestureRecognizer looks for panning (dra ...
- IEEP部署企业级网络工程-OSPF邻居关系故障排除
OSPF邻居关系故障-现象与排除 一.OSPF邻居关系故障-现象与排除 1.OSPF建立邻居关系时,将检验hello报文中的Area ID .Autype.Authentication.network ...
- 基于腾讯云centos简单搭建VSFTP
基于腾讯云centos7.3搭建VSFTP 环境分析: 基于vsftp服务在于云主机上,所以推荐使用FTP的PASV模式: FTP协议有两种工作方式:PORT方式和PASV方式,中文意思为主动式和被动 ...
- shell命令详解
sed命令 将文本input.txt中含有”姓名”字符串的行中的谢朝辉替换成扎巴依 sed -e '/姓名/s/谢朝辉/扎巴依/g' input.txt 将input.txt中第n(5)行替换成”ji ...
- HBase编程 API入门系列之put(客户端而言)(1)
心得,写在前面的话,也许,中间会要多次执行,连接超时,多试试就好了. [hadoop@HadoopSlave1 conf]$ cat regionservers HadoopMasterHadoopS ...
- February 5 2017 Week 6 Sunday
Prosperity discovers vice, adversity virtue. 得意时露瑕疵,逆境中见品质. Adversity will discover your virtue, not ...
- hdu 6208 The Dominator of Strings【AC自动机】
hdu 6208 The Dominator of Strings[AC自动机] 求一个串包含其他所有串,找出最长串去匹配即可,但是匹配时要对走过的结点标记,不然T死QAQ,,扎心了.. #inclu ...
- Ajax实例二:取得新内容
Ajax实例二:取得新内容 通过点击pre和next按钮,从服务器取得最新内容. HTML代码 <div id="slide">图片显示区</div> &l ...
- ringMVC——redirect重定向跳转传值
spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参数跳转,页面也能显示. 首先先来介绍一下不带参数的重定向: ...