【发布时间】:2018-07-13 03:28:38
【问题描述】:
我需要帮助上传照片(以及编辑器中的其他插入数据),但仅在单击提交按钮时。 我搜索了论坛,并用谷歌搜索但没有运气:(
我正在使用的代码用于上传图像并将文本保存到数据库,但是当我将它添加到编辑器时它会立即上传图像。 这不是我想要的行为,'因为如果用户将图像添加到编辑器然后决定关闭选项卡/关闭浏览器或转到另一个地址,图像将存储在服务器上 - 所以我希望有人帮助我只上传图像按下提交按钮时(在此之前,它将仅作为预览出现)。 这是我的代码:
$('#summernote').summernote({
//placeholder: 'your Message',
height: 200,
toolbar: [
['style', ['style']],
['font', ['bold', 'italic', 'underline', 'clear']],
['fontname', ['fontname']],
['color', ['color']],
['para', ['ul', 'ol', 'paragraph']],
['height', ['height']],
['table', ['table']],
['insert', ['link', 'picture', 'hr']],
['view', ['fullscreen', 'codeview', 'help']],
['save-button', ['save']]
],
callbacks : {
onImageUpload: function(image) {
uploadImage(image[0]);
}
}
});
function uploadImage(image) {
var slika = new FormData();
slika.append("image",image);
$.ajax ({
data: slika,
type: "POST",
url: "url - upload image script",// this file uploads the picture and
// returns a chain containing the path
cache: false,
contentType: false,
processData: false,
success: function(url) {
var image = url;
$('#summernote').summernote("insertImage", image);
console.log(slika);
},
error: function(data) {
console.log(slika);
}
});
}
$(".note-save-button").addClass("pull-right");
$(function(){
$('#addit_dtls_form').submit(function(event){
var input_content = $('#summernote').summernote('code');
var is_empty = $("#is_empty").val();
var location_id = $("#location_id").val();;
//event.preventDefault();
$.ajax({
url: 'url - store text to database',
type: 'post',
data: {
'input_content' : input_content,
'is_empty' : is_empty,
'location_id' : location_id,
},
success: function(response){
$.smallBox({
title : "USPEŠNO!",
content : "Sadržaj je uspešno snimljen!",
color : "#7DC27D",
timeout: 4000,
icon : "fa fa-check"
});
//console.log(input_content);
}
});
});
});
希望有人可以帮助我,或者有人可以指出一些示例代码。 提前Tnx!
【问题讨论】:
-
我想做同样的事情——我的图片上传工作正常,但是对于非常大的图片需要几秒钟。我宁愿图像立即显示在编辑器中(就像他们在 base64 和默认行为时所做的那样),然后仅在提交编辑器文本时才上传。来到这里希望你能找到答案。 :-) 如果我能解决这个问题,我会告诉你的......
标签: jquery ajax summernote