使用 CKEditor
上传图像插件。
演示
我们也遇到了同样的问题。添加了插件和图像上传和下载 API 操作。
然后从编辑器中删除过去的按钮。
config.removeButtons = 'Paste,PasteText,PasteFromWord';
将以下代码添加到CKEditor的config.js文件中。
config.extraPlugins = 'uploadimage'
config.uploadUrl = '/uploader/upload.php'
config.filebrowserUploadUrl = '/uploader/upload.php'
接着,使用CTRL + V将图像从Word文档粘贴到此处。
我正在使用MVC5。因此配置如下:
config.extraPlugins = 'uploadimage';
config.uploadUrl = '/CkEditorUploadSupport/UploadImage';
config.filebrowserUploadUrl = '/CkEditorUploadSupport/UploadImage';
MVC控制器代码;(项目控制器文件夹下的控制器名称为“CkEditorUploadSupport”)
public JsonResult UploadImage()
{
if (System.Web.HttpContext.Current.Request.Files.AllKeys.Any())
{
var file = System.Web.HttpContext.Current.Request.Files["upload"];
var targetLocation = @"D:\CKTestFolder";
if (!Directory.Exists(targetLocation))
{
Directory.CreateDirectory(targetLocation);
}
var pattern = new Regex(@"[:!@#$%^&*()}{|\"":?><\[\]\\;'/,~]");
var modifiedFileName = pattern.Replace(file.FileName, "");
modifiedFileName = modifiedFileName.Replace("\"", " ");
modifiedFileName = modifiedFileName.Replace("4â€Â", " ");
var physicalPath = Path.Combine(targetLocation, modifiedFileName);
var fileName = Path.GetFileName(physicalPath);
var newName = fileName;
while (System.IO.File.Exists(physicalPath))
{
var newFileName = Path.GetFileNameWithoutExtension(fileName)
+ "_" + RandomString(3) +
Path.GetExtension(fileName);
physicalPath = Path.Combine(targetLocation, newFileName);
newName = newFileName;
}
file.SaveAs(physicalPath);
var response = new
{
uploaded = 1,
fileName = newName,
url = "/CkEditorUploadSupport/OpenImage?imageName=" + newName
};
return Json(response);
}
var response2 = new
{
uploaded = 0,
message = "Upload Error.."
};
return Json(response2);
}
public ActionResult OpenImage(string imageName)
{
var targetLocation = @"D:\CKTestFolder";
var physicalPath = Path.Combine(targetLocation, imageName);
if (!System.IO.File.Exists(physicalPath))
{
var response2 = new
{
uploaded = 0,
message = "File Not Found"
};
return Json(response2);
}
string mimeType = MimeMapping.GetMimeMapping(imageName);
return base.File(physicalPath, mimeType);
}
private static Random random = new Random();
public static string RandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}