简体   繁体   中英

Paste image from clipboard javascript

We have a program the wants to get an image copy into the clipboard to paste into a file on the HDD using javascript / HTA .

Does anyone done something like this before? Or How can I create an image file on the HDD from the information in the clipboard?

We are already using the following for text, but it does not work for images:

clipboardData.setData("Text", 'To Copy to clipboard');
clipboardData.getData("Text");  // To copy from clipboard

You can possibly read the clipboard data in some supported browsers:

Is it possible to read the clipboard in Firefox, Safari and Chrome using Javascript?

The problem is with you storing this data on the user's hard-drive. Javascript to my knowledge will not give you access to the user's hard-drive due to security reasons. One way to get around this is to send this data to a server running a php script that will then proceed to read the data and save it to the server's local storage. This php script can be set up to return the full path which was used when saving the file. Your javascript post method can then use this returned path to load it in a browser which will prompt your browser to display the download prompt. Then the user can download the file and save it to their local drive.

It very convoluted but can work.

RE: HTA

HTA only works in IE and is not very popular so you will have some problems finding code resource for the exact tasks that you require. This is some code which I found for reading and writing files to disk

<!--
// CAREFUL -- no error checking
function readFile()
{
   var fso, fileHandle, contents, yourfilename;
   fso = new ActiveXObject("Scripting.FileSystemObject");   
   fileHandle = fso.OpenTextFile(document.editor.yourfilename.value, 1); 
   contents = fileHandle.ReadAll();                       

   if (contents)
     document.all("fileContents").value = contents;    

   fileHandle.close();

 }

function writeFile()
{
   var fso, fileHandle, yourfilename;
   fso = new ActiveXObject("Scripting.FileSystemObject");
   fileHandle = fso.CreateTextFile(document.editor.yourfilename.value, true);     
   fileHandle.write(document.all("fileContents").value);          
   fileHandle.close();   
}


//-->

Then you will have to combine this code to use the window.clipboardData.getData functionality for getting the stored clipboard contents. I have never done HTA so I can't give you any help with that.

It look that is impossible to paste an image from the clipboard. We ended up using via command line an external application like Minicap ( http://www.softpedia.com/get/Multimedia/Graphic/Graphic-Capture/MiniCap.shtml ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM