參考:https://www.cnblogs.com/Scholars/p/8968838.htmljavascript
下載:http://www.wangeditor.com/html
前端代碼:前端
<script type="text/javascript"> //下面這兩行腳本就是彈出文本框 var E = window.wangEditor var editor = new E('#editor') // 上傳圖片(舉例) editor.customConfig.uploadImgServer = '/upload.ashx' //將網絡圖片隱藏掉 editor.customConfig.showLinkImg = false // 將 timeout 時間改成 3s editor.customConfig.uploadImgTimeout = 1000 * 10; document.getElementById('btn1').addEventListener('click', function () { // 讀取 html alert(editor.txt.html()) }, false) editor.create(); </script> <body> <form id="newspost" method="post" action="newspost" enctype="multipart/form-data"> <input type="hidden" id="content" name="content"/> <div style="padding: 5px 0; color: #ccc"></div> <div id="editor"></div> <br/> </form> <button id="btn1">獲取html</button> </body>
後端代碼(通常處理程序):java
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; namespace WebApplication1 { /// <summary> /// upload 的摘要說明 /// </summary> public class upload : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.Charset = "utf-8"; var files = context.Request.Files; if (files.Count <= 0) { return; } HttpPostedFile file = files[0]; if (file == null) { context.Response.Write("error|file is null"); return; } else { string Url = "http://192.168.0.20:8099/IMG/"; string path = context.Server.MapPath("/Upader/Img/"); //存儲圖片的文件夾 if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string originalFileName = file.FileName; string fileExtension = originalFileName.Substring(originalFileName.LastIndexOf('.'), originalFileName.Length - originalFileName.LastIndexOf('.')); string currentFileName = (new Random()).Next() + fileExtension; //文件名中不要帶中文,不然會出錯 //生成文件路徑 string imagePath = path + currentFileName; //保存文件 file.SaveAs(imagePath); //獲取圖片url地址 string imgUrl = "./Upader/Img/" + currentFileName; string Json = "{\"data\": [\"../../" + imgUrl.Replace(@"\", @"/") + "\"],\"errno\":\"0\"}"; //返回圖片url地址 context.Response.Write(Json); return; } } public bool IsReusable { get { return false; } } } } 通常處理程序代碼