Advertisement
Guest User

Untitled

a guest
Jun 25th, 2014
938
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 4.36 KB | None | 0 0
  1. $('document').ready(function(){
  2.  
  3. window.addEventListener("paste", pasteHandler);
  4.  
  5. function pasteHandler(e){
  6.     if(e.clipboardData && $("#ctl00_PlaceHolderMain_PageContent_RichHtmlField_hiddenDisplay")[0]) {
  7.        var items = e.clipboardData.items;
  8.         if (items){
  9.             for (var i = 0; i < items.length; i++) {
  10.                if (items[i].type.indexOf("image") !== -1) {
  11.                    var blob = items[i].getAsFile();
  12.                    UploadMe(blob);
  13.                    }
  14.                }
  15.            }
  16.  
  17.        }
  18.  
  19.    }
  20.  
  21. }); //end of doc.ready
  22.  
  23. function UploadMe(readFile) {
  24.    var reader = new FileReader();
  25.    reader.readAsArrayBuffer(readFile); //array buffer
  26.    reader.onprogress = updateProgress;
  27.    reader.onload = loaded;
  28.    reader.onerror = errorHandler;
  29. }
  30.  
  31. function loaded(evt) {
  32.    var fileString = evt.target.result;
  33.    var X = _arrayBufferToBase64(fileString); // this is the mothod to convert Buffer array to Binary
  34.        //var fileInput = document.getElementById('attafh');
  35.        //var fileDisplayArea = document.getElementById('fileDisplayArea');
  36.        var file = guid() + ".PNG" //fileInput.values;
  37.        var filePath = "From Clipboard" //$('#attafh').val(); // "c:\\test.pdf";
  38.        var pageID = _spPageContextInfo.pageItemId;
  39.        //var file = filePath.match(/\\([^\\]+)$/)[1];
  40.  
  41.        var soapEnv =
  42.            "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> \
  43.                 <soap:Body>\
  44.                     <CopyIntoItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>\
  45.                         <SourceUrl>" + filePath + "</SourceUrl>\
  46.                             <DestinationUrls>\
  47.                                 <string>https://domain.com/wikis/ITS/PastedImages/" + file + "</string>\
  48.                             </DestinationUrls>\
  49.                             <Fields>\
  50.                                 <FieldInformation Type='Text' DisplayName='Title' InternalName='Title' Value='Pasted Image'  />\
  51.                                 <FieldInformation Type='Text' DisplayName='PageID' InternalName='PageID' Value='" + pageID + "'  />\
  52.                             </Fields>\
  53.                         <Stream>" + X + "</Stream>\
  54.                     </CopyIntoItems>\
  55.                 </soap:Body>\
  56.             </soap:Envelope>";
  57.  
  58.         $.ajax({
  59.             url: "https://domain.com/wikis/ITS/_vti_bin/copy.asmx",
  60.             beforeSend: function (xhr) { xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/CopyIntoItems"); },
  61.             type: "POST",
  62.             dataType: "xml",
  63.             data: soapEnv,
  64.             complete: function(){
  65.                 insertHtmlAtCursor("<img src='https://domain.com/wikis/ITS/PastedImages/" + file + "'/>");
  66.             },
  67.             contentType: "text/xml; charset=\"utf-8\""
  68.         });
  69.  
  70.     }
  71.     //SP.SOD.executeOrDelayUntilScriptLoaded(initialize, 'SP.js');
  72.     //SP.SOD.executeOrDelayUntilScriptLoaded(test, 'SP.js');
  73.     //SP.SOD.executeFunc('sp.js', 'SP.ClientContext', initialize);
  74.     //SP.SOD.executeFunc('sp.js', 'SP.ClientContext', test);
  75.  
  76.  
  77.     function errorHandler(evt) {
  78.         if (evt.target.error.name == "NotReadableError") {
  79.             // The file could not be read.
  80.         }
  81.     }
  82.  
  83.     function _arrayBufferToBase64(buffer) {
  84.         var binary = ''
  85.         var bytes = new Uint8Array(buffer)
  86.         var len = bytes.byteLength;
  87.         for (var i = 0; i < len; i++) {
  88.            binary += String.fromCharCode(bytes[i])
  89.        }
  90.        return window.btoa(binary);
  91.    }
  92.  
  93.    function updateProgress(evt) {
  94.    }
  95.  
  96.    function guid() {
  97.      function s4() {
  98.        return Math.floor((1 + Math.random()) * 0x10000)
  99.                   .toString(16)
  100.                   .substring(1);
  101.      }
  102.      return s4() + s4() + s4();
  103.    }
  104.  
  105.    function insertHtmlAtCursor(html) {
  106.        var range, node;
  107.        if (window.getSelection && window.getSelection().getRangeAt) {
  108.            range = window.getSelection().getRangeAt(0);
  109.            node = range.createContextualFragment(html);
  110.            range.insertNode(node);
  111.        } else if (document.selection && document.selection.createRange) {
  112.            document.selection.createRange().pasteHTML(html);
  113.    }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement