Is there a way to read image files from url using only vanilla javascript? As context, I'm building a simple drag and drop image uploader and I think I've got the part where you read a literal file from a PC, but as for URLs how can I do it? Example: https://imgur.com/upload
I would like to be able to drag an image from google and have it read in the dropzone.
https://codepen.io/Ryenra/pen/JjoyPaq
function dropEv(e) {
e.preventDefault();
e.stopPropagation();
}
function dragOver(e) {
document.getElementById('box').style = "opacity: 0.9;";
}
function dragLeave(e) {
document.getElementById('box').style.removeProperty('opacity');
}
function receiveFile(e) {
let temp = e.dataTransfer.files;
if(temp.length && temp[0].type.match(/image.*/)){
document.getElementById('eText').textContent = temp[0].name;
}else{
alert('nv');
}
}
html,body{
height: 100%
}
#content {
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
#box{
width: 350px;
height: 300px;
background: repeating-linear-gradient(
-55deg,
#222,
#222 10px,
#333 10px,
#333 20px
);
display: flex;
justify-content: center;
align-items: center;
}
#opa{
width: 100%;
height: 100%;
border: 2px solid #000;
display: flex;
justify-content: center;
align-items: center;
}
#inputFile{
display: none;
}
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="content">
<div id="box" ondrop="dropEv(event),dragLeave(event),receiveFile(event)" ondragover="dragOver(event),dropEv(event)" ondragleave="dragLeave(event)">
<div id= "opa">
<p id="eText">Choose an image or drag it here!</p>
</div>
</div>
</div>
</body>
</html>