最近,因為一些需求要用到 Markdown Editor 套件來製作線上版的 Markdown 的編輯器,我看到一套不錯的,但是他圖片上傳是用 php 所以我就改寫一下,如果有需要的就自行取用..
我用的是這一套 Editor.md

source code :
https://github.com/pandao/editor.md
1. 首先他有一個原本的 image upload sample code 在 /examples/image-upload.html 配合 /examples/php/upload.php 所以我們得將 upload.php 改成 .net 版本。
2. 撰寫檔案上傳,這邊我就很單純撰 把圖片收到後 建立在 /source/該檔案,所以我建立一個 uploadimage.ashx
Source code :
using System;
using System.IO;
using System.Web;
using System.Web.Script.Serialization;
namespace MDEditorUploadImage
{
public class uploadimage : IHttpHandler
{
public class ResultInfo
{
public int success { get; set; }
public string message { get; set; }
public string url { get; set; }
}
public void ProcessRequest(HttpContext context)
{
try
{
//儲存路徑
DirectoryInfo FileDir = new DirectoryInfo(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "source\\");
//如果檔案夾沒有建立則自行建立
if (FileDir.Exists == false)
{
FileDir.Create();
}
HttpFileCollection uploadedFiles = context.Request.Files;
if (uploadedFiles.Count > 0)
{
HttpPostedFile userPostedFile = uploadedFiles[0];
if (userPostedFile.ContentLength > 0)
{
userPostedFile.SaveAs(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "source" + Path.DirectorySeparatorChar + Path.GetFileName(userPostedFile.FileName));
context.Response.Write(new JavaScriptSerializer().Serialize(new ResultInfo { message = "上傳成功", success = 1, url = "http://"+context.Request.Url.Authority + "/source/" + Path.GetFileName(userPostedFile.FileName) }));
}
}
}
catch (Exception ex)
{
context.Response.Write(new JavaScriptSerializer().Serialize(new ResultInfo { message = "上傳失敗:"+ex.Message, success = 0, url ="" }));
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
3.這時候 修改前端 HTML :
Source Code :
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8" />
<title>图片上传示例 - Editor.md examples</title>
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="css/editormd.css" />
<link rel="shortcut icon" href="https://pandao.github.io/editor.md/favicon.ico" type="image/x-icon" />
</head>
<body>
<div id="layout" style="height: 2000px;background: #f6f6f6;">
<header>
<h1>圖片上傳範例 by ASPX</h1>
<p>Image upload example</p>
</header>
<div id="test-editormd">
<textarea style="display:none;">#### Settings
```javascript
{
imageUpload : false,
imageFormats : ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
imageUploadURL : "uploadimage.ashx",
}
```
#### JSON data
```json
{
success : 0 | 1, // 0 表示上傳失敗,1 表示上傳成功
message : "提示的信息,上傳成功或上傳失敗及錯誤信息等。",
url : "圖片地址" // 上傳成功時才返回
}
```
> Source sample : https://github.com/pandao/editor.md
</textarea>
</div>
</div>
<script src="js/jquery.min.js"></script>
<script src="editormd.js"></script>
<script type="text/javascript">
$(function () {
var testEditor = editormd("test-editormd", {
width: "90%",
height: 640,
markdown: "",
path: '../lib/',
//dialogLockScreen : false, // 設置彈出層對話框不鎖屏,全局通用,默認為 true
//dialogShowMask : false, // 設置彈出層對話框顯示透明遮罩層,全局通用,默認為 true
//dialogDraggable : false, // 設置彈出層對話框不可拖動,全局通用,默認為 true
//dialogMaskOpacity : 0.4, // 設置透明遮罩層的透明度,全局通用,默認值為 0.1
//dialogMaskBgColor : "#000", // 設置透明遮罩層的背景顏色,全局通用,默認為 #fff
imageUpload: true,
imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
imageUploadURL: "uploadimage.ashx",
/*
上傳的後台只需要返回一個 JSON 數據,結構如下:
{
success : 0 | 1, // 0 表示上傳失敗,1 表示上傳成功
message : "提示的信息,上傳成功或上傳失敗及錯誤信息等。",
url : "圖片地址" // 上傳成功時才返回
}
*/
});
});
</script>
</body>
</html>
Result :

Download Source Code :
https://github.com/donma/MDEditorUploadImage