Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function handlePaste(event) {
- // Get the clipboard data from the event
- var clipboardData = event.clipboardData || window.clipboardData;
- // Get the first item from the clipboard data
- var item = clipboardData.items[0];
- // Check if the item is an image
- if (item && item.type.indexOf("image") !== -1) {
- // Get the image file as a blob
- var blob = item.getAsFile();
- // Create a new file object with the same blob, name and type as the original file
- var file = new File([blob], blob.name, {type: blob.type});
- // Create a new form data object
- var formData = new FormData();
- // Append the file to the form data with the key "image"
- formData.append("image", file);
- // Create a new XMLHttpRequest object
- var xhr = new XMLHttpRequest();
- // Set the request method to POST and the URL to "/"
- xhr.open("POST", "/");
- // Set the onload function to handle the response
- xhr.onload = function() {
- // Check if the status is 200 OK
- if (xhr.status === 200) {
- // Parse the response as JSON
- var data = JSON.parse(xhr.responseText);
- // Get the image URL from the data
- var url = data.url;
- // Create a new image element
- var img = document.createElement("img");
- // Set the src attribute to the image URL
- img.src = url;
- // Set the class attribute to "image"
- img.className = "image";
- // Create a new div element
- var div = document.createElement("div");
- // Set the class attribute to "url"
- div.className = "url";
- // Set the text content to the image URL
- div.textContent = url;
- // Get the container element by its ID
- var container = document.getElementById("container");
- // Append the image and the div elements to the container element
- container.appendChild(img);
- container.appendChild(div);
- } else {
- // Alert an error message
- alert("Something went wrong!");
- }
- };
- // Send the form data as the request body
- xhr.send(formData);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement