小编Rye*_*nra的帖子

如何仅使用 vanilla javascript 从 url 读取图像文件?

有没有一种方法可以仅使用vanilla javascript从 url 读取图像文件?作为上下文,我正在构建一个简单的拖放图像上传器,我想我已经有了从 PC 读取文字文件的部分,但至于 URL,我该怎么做呢?示例: https: //imgur.com/upload

我希望能够从谷歌拖动图像并在拖放区中读取它。

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');
    }
}
Run Code Online (Sandbox Code Playgroud)
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 …
Run Code Online (Sandbox Code Playgroud)

html javascript

6
推荐指数
1
解决办法
808
查看次数

标签 统计

html ×1

javascript ×1