简体   繁体   English

如何在javascript中下载图像并转换为base64?

[英]How do I download an image and convert to base64 in javascript?

Can I post an Image to another page without downloading it?我可以在不下载的情况下将图像发布到另一个页面吗? (convert to base64 and post) (转换为 base64 并发布)

function run(){
  const url = 'http://g1.gangsters.pl/php/ks_sa_100.php?co=61';
  const body = new FormData()
  body.append('blokada', true)
  body.append('y', Math.floor(Math.random() * (100 - 100 + 1)) + 100)

  fetch(url, { method: 'POST', body })
}

setInterval(() => {
  console.log('Robbing....')
  run()
}, 1700)

How do I add something to download this image, http://g1.gangsters.pl/php/sec_token.php?type=sa a then convert it to a base64 string?我如何添加一些东西来下载这个图像, http ://g1.gangsters.pl/php/sec_token.php?type=sa a 然后将它转换为 base64 字符串?

If I got your question right如果我问对了你的问题

  • first get an image into base64,首先将图像放入base64,
  • send afterwards that base64 within a body of a POST request之后在 POST 请求的body中发送 base64

You could first wait (using Promise) that a FileReader reads the imageData.您可以先等待(使用 Promise)FileReader 读取 imageData。 .then pass the base64 to your next (post) function: .then将 base64 传递给您的下一个(发布)函数:

const toDataURL = (url) => fetch(url)
  .then(res => res.blob())
  .then(blob => new Promise((res, err) => {
    const FR = new FileReader();
    FR.onloadend = () => res(FR.result);
    FR.readAsDataURL(blob);
    FR.onerror = err;
  }));

const toSecToken = toDataURL('http://g1.gangsters.pl/php/sec_token.php?type=sa')
  .then(base64 => {
    const body = new FormData();
    body.append('base64', base64);
    body.append('blokada', true);
    body.append('y', Math.floor(Math.random() * (100 - 100 + 1)) + 100);
    return fetch('http://g1.gangsters.pl/php/ks_sa_100.php?co=61', { method:'POST', body });
  });

toSecToken.then(res => {
    console.log(res);
});

As noted by others, this should be better handled on the server side.正如其他人所指出的,这应该在服务器端更好地处理。 Sending an image back and forth (+ the 1/4 of added weight since the base64 encoding - and a server that allows CORS...) seems like an unnecessary overhead.来回发送图像(+ 自 base64 编码以来增加的重量的 1/4 - 以及允许 CORS 的服务器......)似乎是不必要的开销。
So the sec_token.php could be extended to handle the image forwarding to ks_sa_100.php (+ the necessary query params like co=61 or whatever is needed)所以sec_token.php可以扩展到处理图像转发到ks_sa_100.php (+ 必要的查询参数,如co=61或任何需要的)

# ks_sa_100.php?co=61

$img = file_get_contents('http://g1.gangsters.pl/php/sec_token.php?type=sa');  
$base64 = base64_encode($img); // if you really need that base64 data string

Resources:资源:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises
https://developer.mozilla.org/en-US/docs/Web/API/FileReader https://developer.mozilla.org/en-US/docs/Web/API/FileReader
https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

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

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