一個(gè)簡(jiǎn)單不報(bào)錯(cuò)的summernote 圖片上傳案例
來源:易賢網(wǎng) 閱讀:3229 次 日期:2016-07-28 16:04:32
溫馨提示:易賢網(wǎng)小編為您整理了“一個(gè)簡(jiǎn)單不報(bào)錯(cuò)的summernote 圖片上傳案例”,方便廣大網(wǎng)友查閱!

一個(gè)比較完整的summernote上傳圖片的案例,沒有后臺(tái)(上傳圖片網(wǎng)上案例太多),只有前端js.修正了網(wǎng)上提供的,但是有bug的代碼。

這個(gè)例子,js保證不報(bào)錯(cuò)。親測(cè)可用

<%@ page language="java" contentType="text/html; charset=utf-8"

  pageEncoding="utf-8"%>

<!DOCTYPE html >

<html>

<head>

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>

<link rel="stylesheet" type="text/css">

<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.1/js/bootstrap.min.js"></script>

<link rel="stylesheet" type="text/css"><!--必須-->

<link href="summernote-master/dist/summernote.css" rel="stylesheet" type="text/css"><!--必須-->

<script src="summernote-master/dist/summernote.js"></script><!--必須-->

<script src="summernote-master/lang/summernote-zh-CN.js"></script>

<title>bootstrap-markdown</title>

<style>

.note-alarm {

float: right;

margin-top: 10px;

margin-right: 10px;

}

</style>

</head>

<body>

 <div id="summernote"></div>

<script type="text/javascript">

$(document).ready(function() {

/* function sendFile(file, editor,welEditable) {

 console.log("file="+file);

 console.log("editor="+editor);

 console.log("welEditable="+welEditable);

  data = new FormData();

  data.append("blog_image[image]", file);

  $.ajax({

   url: 'blog_images.jsp',

   data: data,

   cache: false,

   contentType: false,

   processData: false,

   type: 'POST',

   success: function(data){

    editor.insertImage(welEditable, data.url);

   }

  });

 }

*/

 $('#summernote').summernote({

  height: 300, /*高さを指定*/

  lang: 'zh-CN', // default: 'en-US'

 focus:true,

  toolbar: [

   ['style', ['bold', 'italic', 'underline', 'clear']],

   ['fontsize', ['fontsize']],

   ['color', ['color']],

   ['para', ['ul', 'ol', 'paragraph']],

   ['height', ['height']],

   ['insert', ['picture', 'video']]

  ],

 /* image: {

     selectFromFiles: '選擇文件'

    }, */

  /*onImageUpload: function(files, editor, welEditable) {

    sendFile(files[0], editor,welEditable);

  }*/

    onImageUpload: function(files, editor, $editable) {

  sendFile(files[0],editor,$editable);

  }

 });

});

function sendFile(file, editor, $editable){

$(".note-toolbar.btn-toolbar").append('正在上傳圖片');

var filename = false;

try{

filename = file['name'];

alert(filename);

} catch(e){filename = false;}

if(!filename){$(".note-alarm").remove();}

//以上防止在圖片在編輯器內(nèi)拖拽引發(fā)第二次上傳導(dǎo)致的提示錯(cuò)誤

var ext = filename.substr(filename.lastIndexOf("."));

ext = ext.toUpperCase();

var timestamp = new Date().getTime();

var name = timestamp+"_"+$("#summernote").attr('aid')+ext;

//name是文件名,自己隨意定義,aid是我自己增加的屬性用于區(qū)分文件用戶

data = new FormData();

data.append("file", file);

data.append("key",name);

data.append("token",$("#summernote").attr('token'));

$.ajax({

data: data,

type: "POST",

url: "/summernote/fileupload", //圖片上傳出來的url,返回的是圖片上傳后的路徑,http格式

contentType: "json",

cache: false,

processData: false,

success: function(data) {

//data是返回的hash,key之類的值,key是定義的文件名

alert(data);

//把圖片放到編輯框中。editor.insertImage 是參數(shù),寫死。后面的http是網(wǎng)上的圖片資源路徑。

//網(wǎng)上很多就是這一步出錯(cuò)。

$('#summernote').summernote('editor.insertImage', "http://res.cloudinary.com/demo/image/upload/butterfly.jpg");

$(".note-alarm").html("上傳成功,請(qǐng)等待加載");

setTimeout(function(){$(".note-alarm").remove();},3000);

},

error:function(){

$(".note-alarm").html("上傳失敗");

setTimeout(function(){$(".note-alarm").remove();},3000);

}

});

}

</script>

</body>

</html>

以上這篇一個(gè)簡(jiǎn)單不報(bào)錯(cuò)的summernote 圖片上傳案例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)