Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $('document').ready(function(){
- window.addEventListener("paste", pasteHandler);
- function pasteHandler(e){
- if(e.clipboardData && $("#ctl00_PlaceHolderMain_PageContent_RichHtmlField_hiddenDisplay")[0]) {
- var items = e.clipboardData.items;
- if (items){
- for (var i = 0; i < items.length; i++) {
- if (items[i].type.indexOf("image") !== -1) {
- var blob = items[i].getAsFile();
- UploadMe(blob);
- }
- }
- }
- }
- }
- }); //end of doc.ready
- function UploadMe(readFile) {
- var reader = new FileReader();
- reader.readAsArrayBuffer(readFile); //array buffer
- reader.onprogress = updateProgress;
- reader.onload = loaded;
- reader.onerror = errorHandler;
- }
- function loaded(evt) {
- var fileString = evt.target.result;
- var X = _arrayBufferToBase64(fileString); // this is the mothod to convert Buffer array to Binary
- //var fileInput = document.getElementById('attafh');
- //var fileDisplayArea = document.getElementById('fileDisplayArea');
- var file = guid() + ".PNG" //fileInput.values;
- var filePath = "From Clipboard" //$('#attafh').val(); // "c:\\test.pdf";
- var pageID = _spPageContextInfo.pageItemId;
- //var file = filePath.match(/\\([^\\]+)$/)[1];
- var soapEnv =
- "<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/'> \
- <soap:Body>\
- <CopyIntoItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>\
- <SourceUrl>" + filePath + "</SourceUrl>\
- <DestinationUrls>\
- <string>https://domain.com/wikis/ITS/PastedImages/" + file + "</string>\
- </DestinationUrls>\
- <Fields>\
- <FieldInformation Type='Text' DisplayName='Title' InternalName='Title' Value='Pasted Image' />\
- <FieldInformation Type='Text' DisplayName='PageID' InternalName='PageID' Value='" + pageID + "' />\
- </Fields>\
- <Stream>" + X + "</Stream>\
- </CopyIntoItems>\
- </soap:Body>\
- </soap:Envelope>";
- $.ajax({
- url: "https://domain.com/wikis/ITS/_vti_bin/copy.asmx",
- beforeSend: function (xhr) { xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/CopyIntoItems"); },
- type: "POST",
- dataType: "xml",
- data: soapEnv,
- complete: function(){
- insertHtmlAtCursor("<img src='https://domain.com/wikis/ITS/PastedImages/" + file + "'/>");
- },
- contentType: "text/xml; charset=\"utf-8\""
- });
- }
- //SP.SOD.executeOrDelayUntilScriptLoaded(initialize, 'SP.js');
- //SP.SOD.executeOrDelayUntilScriptLoaded(test, 'SP.js');
- //SP.SOD.executeFunc('sp.js', 'SP.ClientContext', initialize);
- //SP.SOD.executeFunc('sp.js', 'SP.ClientContext', test);
- function errorHandler(evt) {
- if (evt.target.error.name == "NotReadableError") {
- // The file could not be read.
- }
- }
- function _arrayBufferToBase64(buffer) {
- var binary = ''
- var bytes = new Uint8Array(buffer)
- var len = bytes.byteLength;
- for (var i = 0; i < len; i++) {
- binary += String.fromCharCode(bytes[i])
- }
- return window.btoa(binary);
- }
- function updateProgress(evt) {
- }
- function guid() {
- function s4() {
- return Math.floor((1 + Math.random()) * 0x10000)
- .toString(16)
- .substring(1);
- }
- return s4() + s4() + s4();
- }
- function insertHtmlAtCursor(html) {
- var range, node;
- if (window.getSelection && window.getSelection().getRangeAt) {
- range = window.getSelection().getRangeAt(0);
- node = range.createContextualFragment(html);
- range.insertNode(node);
- } else if (document.selection && document.selection.createRange) {
- document.selection.createRange().pasteHTML(html);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement