图片自动上传

This commit is contained in:
yuanke 2016-05-11 11:40:01 +08:00
parent e502aa7c43
commit 135e02504e
5 changed files with 46 additions and 33 deletions

View File

@ -1,4 +1,4 @@
source 'https://ruby.taobao.org'
source 'https://rubygems.org/'
#source 'http://ruby.sdutlinux.org/'
unless RUBY_PLATFORM =~ /w32/
@ -40,6 +40,8 @@ gem 'kaminari'
gem 'elasticsearch-model'
gem 'elasticsearch-rails'
gem 'certified'
group :development do
gem 'grape-swagger'
gem 'better_errors', '~> 1.1.0'

View File

@ -5,7 +5,7 @@ PATH
rack (~> 1.5.1)
GEM
remote: https://ruby.taobao.org/
remote: https://rubygems.org/
specs:
byebug (4.0.5)
columnize (= 0.9.0)

View File

@ -119,16 +119,41 @@ class Kindeditor::AssetsController < ApplicationController
render :text => @result.to_json
end
#在KE里面复制粘贴的时候 把图片下载到服务器上
def uploadpic
picstr = params["pic"];
picStr = params["pic"]
picArry = picStr.split("|")
picArry = picstr.split("|")
#图片存放目录
dirPath = "public/files/uploads/image/"
dirDate = Time.now.strftime('%Y%m')
picArry.each do |picurl|
puts picurl
unless Dir.exist?(dirPath+dirDate)
Dir.mkdir(dirPath+dirDate)
end
fileStr = []
picArry.each_with_index do |picUrl,index|
imgBaseName = File.basename(picUrl)
tmpImgType = imgBaseName.split(".").last[0..2]
# 仅支持如下图片格式:bmp,jpeg,jpg,png,gif,BMP,JPEG,JPG,PNG,GIF
fileType = tmpImgType.match("gif|jpg|jpeg|png|bmp|BMP|JPEG|JPG|PNG|GIF")
fileName = fileType.nil? ? Time.now.to_i.to_s+index.to_s : Time.now.to_i.to_s+index.to_s+"."+fileType[0]
# fileType = File.basename(picurl).split(".").last[0..2]
# fileName = Time.now.to_i.to_s+index.to_s
fileDir = dirPath + dirDate + "/"+fileName
#重点!!
File.open(fileDir,'wb'){ |f| f.write(open(picUrl).read) }
fileDir = fileDir.split("public")[1]
fileStr = filedir + "|"
end
render :text =>fileStr
end
private

View File

@ -4689,7 +4689,6 @@ function _bindNewlineEvent() {
});
K(doc).keyup(function(e) {
if (e.which != 13 || e.shiftKey || e.ctrlKey || e.altKey) {
df();
return;
}
if (newlineTag == 'br') {
@ -5995,6 +5994,9 @@ _plugin('core', function(K) {
}
}
self.insertHtml(html, true);
df();
// $("#ajax-indicator").hide();
}
K(doc.body).bind('paste', function(e){
if (self.pasteType === 0) {

View File

@ -354,16 +354,7 @@ function enablePasteImg(_editor) {
};
function df() {
var haspicContainer = document.getElementById("has_pic");
if (haspicContainer == null) {
haspicContainer = document.createElement("div");
haspicContainer.id = "has_pic";
haspicContainer.innerHTML = "<input type='text' id='piclist' value='' style='display:none;'/><div id='upload'><b>您有图片需要上传到服务器</b>&nbsp;&nbsp;<a href='javascript:uploadpic();' >上传</a></div><div id='confirm'></div>";
$(".ke-toolbar").after(haspicContainer);
}
var img = $(".ke-edit-iframe").contents().find("img");
var piccount = 0;
var sstr = "";
$(img).each(function (i) {
@ -376,26 +367,17 @@ function df() {
sstr += that.attr("src") + "|";
}
});
$("#piclist").val(sstr);
document.getElementById("has_pic").style.display = (piccount > 0) ? "block" : "none";
uploadpic(sstr);
}
function closeupload() {
$("#has_pic").hide();
$("#upload").show();
}
function uploadpic() {
var piclist = encodeURI($("#piclist").val());
function uploadpic(piclist) {
if (piclist.length == 0) return false;
$.ajax({
url: "/kindeditor/uploadpic",
data: "pic=" + piclist,
type: "GET",
beforeSend: function () {
$("#upload").hide();
$("#confirm").text("正在上传中...");
$("#ajax-indicator").text("图片上传中请稍等...");
},
success: function (msg) {
if (msg !== "") {
@ -403,17 +385,19 @@ function uploadpic() {
str = msg.split('|');
var img = $(".ke-edit-iframe").contents().find("img");
var tIndex = 0;
$(img).each(function (i) {
var that = $(this);
if (that.attr("src").indexOf("http://") >= 0 || that.attr("src").indexOf("https://") >= 0) {
that.attr("src", "/uploads/image/" + str[i]);
that.attr("data-ke-src", "/uploads/image/" + str[i]);
that.attr("src", str[tIndex]);
that.attr("data-ke-src", str[tIndex]);
that.parent().attr("href", str[tIndex]);
that.parent().attr("data-ke-src", str[tIndex]);
tIndex = tIndex + 1;
}
});
$("#confirm").html(img.length + "张图片已经上传成功!&nbsp;&nbsp;<a href='javascript:closeupload();'>关闭</a>");
}
else $("#confirm").text("上传失败!");
// else $("#confirm").text("上传失败!");
}
});
}