我使用codeigniter,twitter bootstrap和summernote作爲我的所見即所得編輯器。我面臨圖像上傳的一些問題。使用Summernote上傳圖像時,它會以base64字符串序列化圖像。該base64字符串太長,以至於它不適合phpmyadmin中的文本數據類型。我想要做的是,使用PHP上傳函數上傳圖像,並將其url存儲在數據庫中而不是base64字符串。我會怎麼做?Summernote圖像上傳錯誤
參考this後,這裏的代碼,
$(function() {
$('.summernote').summernote({
height: 100,
onImageUpload: function(files, editor, welEditable) {
sendFile(files[0], editor, welEditable);
}
});
function sendFile(file, editor, welEditable) {
data = new FormData();
data.append("files", file);
upload_url = "<?php echo base_url(); ?>" + "dashboard/uploader/";
$.ajax({
data: data,
type: "POST",
url: upload_url,
cache: false,
contentType: false,
processData: false,
success: function(url) {
editor.insertImage(welEditable, url);
}
});
}
});
在儀表板類上傳方法是我的PHP上傳代碼所在。這裏的PHP代碼,
public function uploader()
{
$this->load->helper('file');
if ($_FILES['files']['name']) {
if (!$_FILES['files']['error']) {
$name = md5(rand(100, 200));
$ext = explode('.', $_FILES['files']['name']);
$filename = $name . '.' . $ext[1];
$destination = base_url().'uploads/' . $filename;
$location = $_FILES["files"]["tmp_name"];
move_uploaded_file($location, $destination);
echo base_url() . $filename;
}
else
{
echo $message = 'Ooops! Your upload triggered the following error: '.$_FILES['files']['error'];
}
}
}
每當我上傳的圖片,它不表現出來的所見即所得的編輯器。我哪裏錯了?
現在看起來是這樣的:
$(function() {
$('.summernote').summernote({
height: 100,
onImageUpload: function(files) {
sendFile(files[0]);
}
});
function sendFile(file) {
data = new FormData();
data.append("files", file);
upload_url = "<?php echo base_url(); ?>" + "dashboard/uploader/";
$.ajax({
data: data,
type: "POST",
url: upload_url,
cache: false,
contentType: false,
processData: false,
success: function(url) {
$(this).summernote("insertImage", url);
}
});
}
});
http://stackoverflow.com/a/35815418/2847436,這可以幫助你。 –