From 54bdaec347d3197e95a04954074d0889e28c16a6 Mon Sep 17 00:00:00 2001 From: meganchen15 Date: Mon, 28 Dec 2015 12:34:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=B8=8A=E4=BC=A0=E5=A4=B4?= =?UTF-8?q?=E5=83=8F=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/blogs_controller.rb | 18 ++++++++++++++++-- app/views/blogs/upload_img.js.haml | 8 ++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100755 app/views/blogs/upload_img.js.haml diff --git a/app/controllers/blogs_controller.rb b/app/controllers/blogs_controller.rb index ef73b44..991dce2 100644 --- a/app/controllers/blogs_controller.rb +++ b/app/controllers/blogs_controller.rb @@ -53,7 +53,19 @@ class BlogsController < ApplicationController end 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 def preview @@ -68,6 +80,8 @@ class BlogsController < ApplicationController protected 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 diff --git a/app/views/blogs/upload_img.js.haml b/app/views/blogs/upload_img.js.haml new file mode 100755 index 0000000..f7a05e3 --- /dev/null +++ b/app/views/blogs/upload_img.js.haml @@ -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]}"); \ No newline at end of file