我需要帮助上传照片(连同编辑器中插入的其他数据),但只有在单击提交按钮时才能上传。
我已经搜索了论坛和谷歌,但没有找到合适的解决方法:(
我正在使用的代码可以上传图像并将文本保存到数据库中,但它会立即上传图像,这对我来说不是期望的行为。因为如果用户在编辑器中添加图像,然后决定关闭选项卡/关闭浏览器或转到另一个地址,图像将存储在服务器上。因此,我希望有人能帮助我仅在按下提交按钮时上传图像(在此之前,它只作为预览存在)。 以下是我的代码:
我正在使用的代码可以上传图像并将文本保存到数据库中,但它会立即上传图像,这对我来说不是期望的行为。因为如果用户在编辑器中添加图像,然后决定关闭选项卡/关闭浏览器或转到另一个地址,图像将存储在服务器上。因此,我希望有人能帮助我仅在按下提交按钮时上传图像(在此之前,它只作为预览存在)。 以下是我的代码:
$('#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);
}
});
});
});
希望有人能帮助我,或者有一些示例代码可以指导我。
先行致谢!