完成上传头像功能

This commit is contained in:
meganchen15 2015-12-28 12:34:18 +08:00
parent a95c79cd6c
commit 54bdaec347
2 changed files with 24 additions and 2 deletions

View File

@ -53,7 +53,19 @@ class BlogsController < ApplicationController
end end
def upload_img def upload_img
@result = {status: false, message: '', text_id: params[:upload][:text_id] || ''}
begin
if params[:upload].present? && params[:upload][:img].present? && remotipart_submitted?
upload_info = upload_picture params[:upload][:img]
@result[:status] = true
@result[:message] = "![#{upload_info[:file_name]}](/images/#{upload_info[:real_file_name]})"
end
rescue UploadException => e
@result[:message] = e.message
end
respond_to do |format|
format.js
end
end end
def preview def preview
@ -68,6 +80,8 @@ class BlogsController < ApplicationController
protected protected
def upload_picture(file) def upload_picture(file)
upload_path = File.join Rails.root, 'public/images'
upload = SimpleFileupload.new upload_path:upload_path, max_size: 1024*1024*2, type: 'image'
upload_info = upload.upload file
end end
end end

View File

@ -0,0 +1,8 @@
- if @result[:status]
:plain
var old_val = $("##{@result[:text_id]}").val();
$("##{@result[:text_id]}").val(old_val + "#{@result[:message]}");
$('#upload_modal').modal('hide');
- else
:plain
$('#error').text("#{@result[:message]}");