zoukankan      html  css  js  c++  java
  • ASP.NET MVC实现剪切板功能

    前言

    关于复制粘贴的功能,好像不用劳师动众的写后端代码,JS就可以,但正如大家所知道的,兼容性问题,当然这么通用的功能怎么可能没有一个通用的方案呢,于是便找到了一款jquery插件 jquery.clip, perfect!但本篇并不是写如何使用jquery.clip,而是通过.net 的Clipboard类来实现。

    代码实现

    一、创建WebAPI,代码如下

    using System.Windows.Forms;
    using System.Threading;
    
    namespace JYZS.Api
    {
        public class ClipboardController : ApiController
        {
            [HttpGet]
            public string CopyToClipboard(string content)
            {
                Thread newThread = new Thread(new ThreadStart(() => { _CopyToClipboard(content); }));
                newThread.SetApartmentState(ApartmentState.STA);
                newThread.Start();
                //newThread.Join();//阻塞调用线程,直到被调用线程结束
                return "";
            }
            private void _CopyToClipboard(string content)
            {
                System.Windows.Forms.Clipboard.SetText(content);
            }
            [HttpGet]
            public string PasteFromClipboard()
            {
                string text = "";
                try
                {
                    Thread newThread = new Thread(new ThreadStart(() => { text = _PasteFromClipboard(); }));
                    newThread.SetApartmentState(ApartmentState.STA);
                    newThread.Priority = ThreadPriority.Highest;
                    newThread.Start();
                    newThread.Join();//阻塞调用线程,直到被调用线程结束
    
                    return text;
                }
                catch
                {
                    return "";
                }
            }
            private string _PasteFromClipboard()
            {
                return System.Windows.Forms.Clipboard.GetText();
            }
        }
    }

    二、前端调用

            function fn_CopyToClipboard() {
                $.ajax({
                    type: 'GET',
                    url: '/api/Clipboard/CopyToClipboard',
                    data: { content: $.trim($('#Content').val()) },
                    success: function (msg) {
                        if (msg == null || msg == '') { alert('已复制到剪切板'); }
                        else alert(msg);
                    },
                    error: function (err) {
                        alert('复制到剪切板失败');
                    }
                });
            }
            function fn_PasteFromClipboard() {
                $.ajax({
                    type: 'GET',
                    url: '/api/Clipboard/PasteFromClipboard',
                    data: { },
                    success: function (msg) {
                        if (msg == null || msg == '') { alert('获取剪切板数据失败'); }
                        else alert(msg);
                    },
                    error: function (err) {
                        alert('获取剪切板数据失败');
                    }
                });
            }

    POST:hope helpful to you!!!

    类名、方法名及命名空间均为本人项目中命名,使用时请注意修改~

  • 相关阅读:
    CompoundButton.OnCheckedChangeListener与RadioGroup.OnCheckedChangeListener冲突
    C# String.Format格式化json字符串中包含"{" "}"报错问题
    在IHttpHandler中获取session
    你真的会玩SQL吗?删除重复数据且只保留一条
    activity结束之后刷新之前的activity的内容
    jQuery打造智能提示插件二(可编辑下拉框)
    byte数组转float 以及byte转其他类型时为什么要&0xff
    为什么byte的取值范围是-128到127
    MySQL修改表、字段、库的字符集及字符集说明
    MySQL分布式jdbc连接
  • 原文地址:https://www.cnblogs.com/njl041x/p/4432442.html
Copyright © 2011-2022 走看看