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