简体   繁体   English

从URL读取图像并上传

[英]Read image from URL & upload

I am not able to find a proper way to read an image from a URL and upload it using JavaScript/Ajax. 我无法找到从URL读取图像并使用JavaScript / Ajax上传图像的正确方法。

Suppose there is a URL: https://pbs.twimg.com/profile_images/1580483969/parisjs_transparent.png . 假设有一个URL: https://pbs.twimg.com/profile_images/1580483969/parisjs_transparent.pnghttps://pbs.twimg.com/profile_images/1580483969/parisjs_transparent.png

I should be able to upload it now, but I guess to upload first I need to download it, so what will be the procedure? 我应该能够立即上传,但我想首先上传我需要下载它,那么程序是什么?

If the remote server allows CORS requests, it possible to download a remote image and read its contents. 如果远程服务器允许CORS请求,则可以下载远程映像并读取其内容。 If the remote server does not allow CORS, the image can be shown (using a standard <img src=".."> tag), but the content cannot be read - the canvas gets tainted . 如果远程服务器不允许CORS,则可以显示图像(使用标准的<img src="..">标记),但无法读取内容 - 画布受到污染

Download image from remote URL using JavaScript when CORS is enabled 启用CORS时,使用JavaScript从远程URL下载映像

 function getImageFormUrl(url, callback) { var img = new Image(); img.setAttribute('crossOrigin', 'anonymous'); img.onload = function (a) { var canvas = document.createElement("canvas"); canvas.width = this.width; canvas.height = this.height; var ctx = canvas.getContext("2d"); ctx.drawImage(this, 0, 0); var dataURI = canvas.toDataURL("image/jpg"); // convert base64/URLEncoded data component to raw binary data held in a string var byteString; if (dataURI.split(',')[0].indexOf('base64') >= 0) byteString = atob(dataURI.split(',')[1]); else byteString = unescape(dataURI.split(',')[1]); // separate out the mime component var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]; // write the bytes of the string to a typed array var ia = new Uint8Array(byteString.length); for (var i = 0; i < byteString.length; i++) { ia[i] = byteString.charCodeAt(i); } return callback(new Blob([ia], { type: mimeString })); } img.src = url; } getImageFormUrl('https://upload.wikimedia.org/wikipedia/commons/thumb/b/b4/The_Sun_by_the_Atmospheric_Imaging_Assembly_of_NASA%27s_Solar_Dynamics_Observatory_-_20100819.jpg/628px-The_Sun_by_the_Atmospheric_Imaging_Assembly_of_NASA%27s_Solar_Dynamics_Observatory_-_20100819.jpg', function (blobImage) { var formData = new FormData(); formData.append('key1', 'value1'); formData.append('key2', 'value2'); formData.append('key2', 'value3'); formData.append('imageToUpload', blobImage); console.log(blobImage); //use formData in "data" property in $.ajax //$.ajax({ //blabla: blabla, //data: formData, //blabla: blabla //}}) }); 

Let server download/upload your image. 让服务器下载/上传您的图像。 What you need to do is to create a text input field for image URL. 您需要做的是为图像URL创建文本输入字段。 Than via AJAX you send to backend the URL only and server gets its contents. 通过AJAX,您只发送到后端的URL,服务器获取其内容。

@DheerajAgrawal @DheerajAgrawal

see this example: http://jsfiddle.net/WQXXT/3004/ 看到这个例子: http//jsfiddle.net/WQXXT/3004/

you should customize that, where it says: 你应该定制它,它说:

alert('start upload script here');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM