簡體   English   中英

如何僅使用 vanilla javascript 從 url 讀取圖像文件?

[英]How to read image files from url using only vanilla javascript?

有沒有辦法只使用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'); } }
 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>

您可以像 Nigel 提到的那樣使用Fetch API

fetch('the image url', {mode: 'cors'})
  .then(res => res.blob())
  .then(blob => { /* use the blob */ })

fetch將返回一個響應,您需要使用res.blob()將其作為Blob讀取

注意: fetchres.blob都返回一個 promise,需要使用示例中的 .then .then()或 async/await 來解決。

BlobFile基本相同,因此您可以根據需要使用Blob ,但如果確實需要,可以使用File() 構造函數將其轉換為File ,如下所示:

let file = new File([blob], "file name", {type: blob.type})

但是,由於存在CORS ,您可能會在嘗試從任意站點獲取圖像時遇到問題。 然后,您可能必須創建一個代理服務器來獲取圖像,並將“Access-Control-Allow-Origin”標頭添加到圖像中,其值為“*”(任何域)或“您的站點所在的域”在”。

對不起,插件,但我最近寫了一篇關於最近在客戶端處理文件的指南,它可能對你的項目有用: Using Files in Client-Side JavaScript

一般來說,你不能。 同源策略防止瀏覽器讀取來自不同源的數據(除非使用 CORS 授予權限)。

您以https://imgur.com/upload為例,但他們不使用客戶端 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'); } }
 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM