ASP. NET MVC implements the Clipboard function

Source: Internet
Author: User

Preface

About the function of copy and paste, as if not belabour write back-end code, JS can, but as you know, compatibility issues, of course, so the general function how can not have a universal solution, so found a jquery plug-in Jquery.clip, perfect! But this article does not write about how to use jquery.clip, but rather through the. NET Clipboard class.

Code Implementation

First, create the WEBAPI, the code is as follows

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 ();//block the calling thread until the called thread ends the 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 ();//block the calling thread until the called thread ends the return text;            } catch {return ""; }} private String _pastefromclipboard () {return System.Windows.Forms.Clipboard.GetText ()        ; }    }}

Second, the front-end call

function Fn_copytoclipboard () {$.ajax ({type: ' GET ', url: '/api/clipboard/copyto                    Clipboard ', data: {content: $.trim ($ (' #Content '). Val ())}, Success:function (msg) {                    if (msg = = NULL | | msg = = ") {alert (' copied to clipboard ');}                else alert (msg);                }, Error:function (err) {alert (' Copy to clipboard failed ');        }            }); } function Fn_pastefromclipboard () {$.ajax ({type: ' GET ', url: '/api/clip Board/pastefromclipboard ', data: {}, Success:function (msg) {if (msg = = NULL | |                    msg = = ') {alert (' Get Clipboard data failed ');}                else alert (msg);                }, Error:function (err) {alert (' Get Clipboard data failed ');        }            }); }
post:hope Helpful to you!!!

Class name, method name and namespace are named in my project, please note that when using the ~

ASP. NET MVC implements the Clipboard function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.