JQuery 复制粘贴上传图片插件(textarea 和 tinyMCE)

开源地址:https://github.com/yuezhongxin/paste-upload-image.js

支持 Ctrl+C/Ctrl+V 上传,支持拖拽上传,也支持 QQ/微信截图上传。

textarea使用(返回markdown格式的图片):

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script src="scripts/jquery.js"></script>
    <script src="scripts/paste-upload-image.js"></script>
</head>
<body>
    <textarea name="txtContent" id="txtContent" style="width:500px;height:200px;"></textarea>
    
    <script>
        $("#txtContent").pasteUploadImage();//bind textarea
    </script>
</body>
</html>

tinyMCE使用:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script src="scripts/jquery.js"></script>
    <script src="tinymce/tinymce.js"></script>
</head>
<body>
    <textarea name="mceContent" id="mceContent"></textarea>

    <script>
        $("#txtContent").pasteUploadImage();

        tinymce.init({
            selector: '#mceContent',
            height: 500,
            plugins: [
              'pasteUpload', //add pasteUpload plugin
              'advlist autolink lists link image charmap print preview anchor',
              'searchreplace visualblocks code fullscreen',
              'insertdatetime media table contextmenu code'
            ],
            toolbar: 'insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image',
            content_css: [
              '//fast.fonts.net/cssapi/e6dc9b99-64fe-4292-ad98-6974f93cd2a2.css',
              '//www.tinymce.com/css/codepen.min.css'
            ]
        });
    </script>
</body>
</html>

后端处理:

public class ImageUploaderController : Controller
{

    [AllowCors("sub.example.com")]//跨域访问
    [HttpPost]
    public async Task<string> ProcessPasteUpload(HttpPostedFileBase imageFile, string mimeType)
    {
        ///to do...
    }
}

public class AllowCorsAttribute : ActionFilterAttribute
{
    private string[] _domains;

    public AllowCorsAttribute(string domain)
    {
        _domains = new string[] { domain };
    }

    public AllowCorsAttribute(string[] domains)
    {
        _domains = domains;
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var context = filterContext.RequestContext.HttpContext;
        var host = context.Request.UrlReferrer?.Host;
        if (host != null && _domains.Contains(host))
        {
            context.Response.AddHeader("Access-Control-Allow-Origin", $"http://{host}");
            context.Response.AddHeader("Access-Control-Allow-Credentials", "true");
        }
        base.OnActionExecuting(filterContext);
    }
}

效果展示:

435188-20160603150528999-774769562.gif

作者: 田园里的蟋蟀 
出处: http://www.cnblogs.com/xishuai/ 
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。




本文转自田园里的蟋蟀博客园博客,原文链接:http://www.cnblogs.com/xishuai/p/jquery-paste-upload-image.html,如需转载请自行联系原作者

Tinymce提供了一些配置选项,可以实现从Word中复制粘贴内容并保留格式和图片的功能。具体实现步骤如下: 1. 加载tinymce的源文件和plugins目录下的插件文件。例如: ```html <script src="/path/to/tinymce.min.js"></script> <script src="/path/to/plugins/paste/plugin.min.js"></script> <script src="/path/to/plugins/paste/plugin.min.js"></script> ``` 2. 设置tinymce的选项,包括使用的主题、插件和工具栏等。例如: ```javascript tinymce.init({ selector: 'textarea', plugins: 'paste image', toolbar: 'undo redo | bold italic | image', paste_data_images: true, images_upload_url: '/path/to/upload', images_upload_handler: function (blobInfo, success, failure) { // 处理图片上传 } }); ``` 其中,paste_data_images选项用于启用从Word中粘贴图片的功能,images_upload_url和images_upload_handler用于处理上传的图片。 3. 在服务器端处理上传的图片,并返回图片的URL地址。例如,使用PHP实现图片上传功能: ```php <?php if ($_FILES['file']['error'] === UPLOAD_ERR_OK) { $file_tmp = $_FILES['file']['tmp_name']; $file_name = $_FILES['file']['name']; $file_ext = pathinfo($file_name, PATHINFO_EXTENSION); $file_path = '/path/to/images/' . uniqid() . '.' . $file_ext; move_uploaded_file($file_tmp, $file_path); echo json_encode(['location' => $file_path]); } else { echo json_encode(['error' => '上传失败']); } ``` 以上就是使用Tinymce实现从Word中复制粘贴内容并保留格式和图片的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值