Fábio Matte Posted March 25, 2019 Share Posted March 25, 2019 I need to copy the text that was selected in the UnidbGrid, through the option of a PopUp menu. I am using the following code: var col: Integer; TextCopy: string; begin col: = gridSenhasLiberadas.CurrCol; Clipboard.AsText: = gridSeedLibrary.Columns.Items [col] .Field.AsString; TextCopy: = Clipboard.AsText; If I give a ShowMessage, I see the Clipboard, however I can not paste it anywhere else. Apparently it's not on the clipboard. How do I then get that dbGrid value and actually put it in the clipboard through a popup menu? Quote Link to comment Share on other sites More sharing options...
Sherzod Posted March 25, 2019 Share Posted March 25, 2019 37 minutes ago, Fábio Matte said: If I give a ShowMessage, I see the Clipboard, however I can not paste it anywhere else. Hi, You are using "Uses Clipbrd", It works on the server side, not on the client. You need JS solution. Please try to search on the forum. Quote Link to comment Share on other sites More sharing options...
Fábio Matte Posted March 25, 2019 Author Share Posted March 25, 2019 6 minutes ago, Sherzod said: Hi, You are using "Uses Clipbrd", It works on the server side, not on the client. You need JS solution. Please try to search on the forum. As I speak Portuguese, I already researched several terms but I could not reach a link to help me. Is there a forum link to tell me or give me a direction? Quote Link to comment Share on other sites More sharing options...
Sherzod Posted March 25, 2019 Share Posted March 25, 2019 Try to search "clipboardData", "execCommand"... Quote Link to comment Share on other sites More sharing options...
Osmanito Posted December 1, 2021 Share Posted December 1, 2021 Use this: UniSession.AddJS('navigator.clipboard.writeText("Hello World!");'); 1 Quote Link to comment Share on other sites More sharing options...
eduardosuruagy Posted November 1, 2022 Share Posted November 1, 2022 On 12/1/2021 at 4:53 PM, Osmanito said: Use this: UniSession.AddJS('navigator.clipboard.writeText("Hello World!");'); Did that work for you? It didn't work for me Quote Link to comment Share on other sites More sharing options...
Hayri ASLAN Posted November 1, 2022 Share Posted November 1, 2022 3 hours ago, eduardosuruagy said: Did that work for you? It didn't work for me Quote Link to comment Share on other sites More sharing options...
eduardosuruagy Posted November 1, 2022 Share Posted November 1, 2022 7 minutes ago, Hayri ASLAN said: Thanks!! Quote Link to comment Share on other sites More sharing options...
wlcardoso Posted March 11, 2023 Share Posted March 11, 2023 EXCELENTE DICA UniSession.AddJS('navigator.clipboard.writeText("'+TextCopy.Text+'");'); O texto pode ter espaço mas tem que ser na mesma linha... Quote Link to comment Share on other sites More sharing options...
Felipe Lugo Posted June 3, 2023 Share Posted June 3, 2023 Hello, I have the following code to paste an image from the clipboard. It works perfectly, but I can't copy the image to a file on the server. I don't know JS, so I appreciate any help you can give me. <script> function retrieveImageFromClipboardAsBlob(pasteEvent, callback){ if(pasteEvent.clipboardData == false){ if(typeof(callback) == "function"){ callback(undefined); } }; var items = pasteEvent.clipboardData.items; if(items == undefined){ if(typeof(callback) == "function"){ callback(undefined); } }; for (var i = 0; i < items.length; i++) { // Skip content if not image if (items[i].type.indexOf("image") == -1) continue; // Retrieve image on clipboard as blob var blob = items[i].getAsFile(); if(typeof(callback) == "function"){ callback(blob); } } } window.addEventListener("paste", function(e){ // Handle the event retrieveImageFromClipboardAsBlob(e, function(imageBlob){ // If there's an image, display it in the canvas if(imageBlob){ var canvas = document.getElementById("mycanvas"); var ctx = canvas.getContext('2d'); // Create an image to render the blob on the canvas var img = new Image(); // Once the image loads, render the img on the canvas img.onload = function(){ // Update dimensions of the canvas with the dimensions of the image canvas.width = this.width; canvas.height = this.height; // Draw the image ctx.drawImage(img, 0, 0); }; // Crossbrowser support for URL var URLObj = window.URL || window.webkitURL; // Creates a DOMString containing a URL representing the object given in the parameter // namely the original Blob img.src = URLObj.createObjectURL(imageBlob); // // Generar un archivo y grabarlo en disco // // Obtener el src de la imagen var src = img.src; // Crear un elemento <a> var a = document.createElement("a"); // Crear una URL para el src var url = window.URL.createObjectURL(src); // Asignar la URL al atributo href del elemento <a> a.href = url; // Asignar el nombre del archivo al atributo download del elemento <a> a.download = "C:\TEMP\imagen.jpg"; // Añadir el elemento <a> al documento document.body.appendChild(a); // Simular un clic en el elemento <a> a.click(); // Remover el elemento <a> del documento document.body.removeChild(a); } }); }, false); </script> <p> Focus this tab and press <kbd>CTRL</kbd> + <kbd>V</kbd>. The image on your clipboard will be rendered on the canvas ! </p> <canvas style="border:1px solid grey;" id="mycanvas"> Quote Link to comment Share on other sites More sharing options...
Sherzod Posted June 3, 2023 Share Posted June 3, 2023 Hello, First of all, please format your post properly. Quote Link to comment Share on other sites More sharing options...
Felipe Lugo Posted June 3, 2023 Share Posted June 3, 2023 An apology for the writing format. I have tried with ajaxRequest and other options, but I have not been able to get the image file to be generated and saved to the local disk. What I want to do is retrieve the image copied to the Clipboard and save it to the DB. Quote Link to comment Share on other sites More sharing options...
Sherzod Posted June 3, 2023 Share Posted June 3, 2023 Thanks. Could you please explain your case in more detail? Quote Link to comment Share on other sites More sharing options...
Felipe Lugo Posted June 3, 2023 Share Posted June 3, 2023 I want to save in a DB an image that was previously copied to the clipboard of the client's browser. I have a uniHTMLFrame where I put the code to do the Paste from Clipboard. This code to make the Paste works OK. The part of the code in bold is the one that does NOT work for me. This part is the one with which I intend to record the image I get in img.src on the server's local disk. With the image saved on the server, I intend to upload it to the DB later. Quote Link to comment Share on other sites More sharing options...
Sherzod Posted June 4, 2023 Share Posted June 4, 2023 On 6/3/2023 at 9:17 PM, Felipe Lugo said: I want to save in a DB an image... This post may help you... Quote Link to comment Share on other sites More sharing options...
Felipe Lugo Posted June 4, 2023 Share Posted June 4, 2023 Thank you very much Sherzod. I already implemented the code that you suggested in the link. I add the example project in case it helps someone else. Kind regards. ejemplo.zip 1 1 Quote Link to comment Share on other sites More sharing options...
picyka Posted June 5, 2023 Share Posted June 5, 2023 A tip for those who want to copy text with few codes, I believe this could be implemented natively in unigui ServerModule -> CustomMeta <script language="JavaScript"> function addslashes(str) { return (str + '').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0'); }; async function copyText(texto) { try { await navigator.clipboard.writeText(texto); } catch (err) { console.error('Failed to copy: ', err); } }; async function pasteText(jsName) { navigator.clipboard.readText().then(texto => { let txt = addslashes(texto); txt = txt.replace(/(?:\r\n|\r|\n)/g, '\\n'); let cmd = jsName + ".setValue('" + txt + "');"; eval(cmd); }); }; </script> procedure TUniMainModule.Copy(Control : TUniFormControl; MsgSucess : Boolean); begin var lTexto := String(Control.Text); Self.CopyStr(lTexto, MsgSucess); end; procedure TUniMainModule.CopyStr(Text: String; MsgSucess: Boolean); begin try var lTexto := TStringUtils.JsEncode(Text); var lJsString := 'copyText(' + QuotedStr(lTexto) + ');'; UniSession.JSCode(lJsString); if MsgSucess then TToastUtils.Sucess('Copiado!',True,'Sucesso',1000); except on E: Exception do TMessageUtils.Error(E.Message); end; end; procedure TUniMainModule.Paste(Control : TUniFormControl); begin try var lJsName := Control.JSName; var lJsString := 'pasteText(' + QuotedStr(lJsName) + ');'; UniSession.JSCode(lJsString); except on E: Exception do TMessageUtils.Error(E.Message); end; end; class function TStringUtils.JsEncode(const Src: String; const useNumericReference: boolean): String; var i, l: integer; Buf, P: PAnsiChar; ch: integer; begin Result := ''; l := Length(Src); if l = 0 then Exit; if useNumericReference then GetMem(Buf, l * 6) // to be on the *very* safe side else GetMem(Buf, l * 2); // to be on the *very* safe side try P := Buf; for i := low(Src) to high(Src) do begin ch := Ord(Src[i]); case ch of 8: begin // Backspace if useNumericReference then begin ALStrMove('\u0008', P, 6); Inc(P, 6); end else begin ALStrMove('\b', P, 2); Inc(P, 2); end; end; 9: begin // Tab if useNumericReference then begin ALStrMove('\u0009', P, 6); Inc(P, 6); end else begin ALStrMove('\t', P, 2); Inc(P, 2); end; end; 10: begin // New line if useNumericReference then begin ALStrMove('\u000A', P, 6); Inc(P, 6); end else begin ALStrMove('\n', P, 2); Inc(P, 2); end; end; 11: begin // Vertical tab if useNumericReference then begin ALStrMove('\u000B', P, 6); Inc(P, 6); end else begin ALStrMove('\v', P, 2); Inc(P, 2); end; end; 12: begin // Form feed if useNumericReference then begin ALStrMove('\u000C', P, 6); Inc(P, 6); end else begin ALStrMove('\f', P, 2); Inc(P, 2); end; end; 13: begin // Carriage return if useNumericReference then begin ALStrMove('\u000D', P, 6); Inc(P, 6); end else begin ALStrMove('\r', P, 2); Inc(P, 2); end; end; 34: begin // Double quote if useNumericReference then begin ALStrMove('\u0022', P, 6); Inc(P, 6); end else begin ALStrMove('\"', P, 2); Inc(P, 2); end; end; 38: begin // & ... we need to encode it because in javascript ' or & will be converted to ' and error unterminated string ALStrMove('\u0026', P, 6); Inc(P, 6); end; 39: begin // Apostrophe or single quote if useNumericReference then begin ALStrMove('\u0027', P, 6); Inc(P, 6); end else begin ALStrMove('\''', P, 2); Inc(P, 2); end; end; 60: begin // < ... mostly to hide all </script> tag inside javascript. // http://www.wwco.com/~wls/blog/2007/04/25/using-script-in-a-javascript-literal/ ALStrMove('\u003C', P, 6); Inc(P, 6); end; 62: begin // > ... mostly to hide all HTML tag inside javascript. ALStrMove('\u003E', P, 6); Inc(P, 6); end; 92: begin // Backslash character (\). if useNumericReference then begin ALStrMove('\u005C', P, 6); Inc(P, 6); end else begin ALStrMove('\\', P, 2); Inc(P, 2); end; end; else Begin P^ := AnsiChar(ch); Inc(P); end; end; end; SetString(Result, Buf, P - Buf); finally FreeMem(Buf); end; end; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.