I'm not strong in JavaScript, but I need to resort to it. The task is not very standard. There is approximately such html:

<img src='http://server.com/?image=RANDOM_STRING' /> 

The image is generated different. I can add attributes to this tag. I need to receive this image and transmit it through a browser, i.e. via JS. There are no problems with sending, only I cannot do anything with receipt. Perhaps this post will help with something: https://stackoverflow.com/questions/934012/get-image-data-in-javascript

  • transfer from where to? - Jean-Claude
  • Getting the image comes from one host, and should be sent to mine. I understand that usually such things are done with other PLs, but, alas, no other option. - Filipp Mustang
  • why not just send the link to the server - what's the point of downloading the image itself? - splash58
  • Images are different each time .. There are no alternative options, unfortunately. - Filipp Mustang

1 answer 1

You can use the latest XMLHttpRequest (previously known as XHR Level 2 ) to receive and send binary data (MDN) . Browser support is pretty good .

If you don’t need to do anything with the data, it’s easiest to handle Blob

Receiving:

 var oReq = new XMLHttpRequest(); oReq.open("GET", "/myfile.png", true); oReq.responseType = "blob"; oReq.onload = function(oEvent) { var blob = oReq.response; // можно использовать blob где-то ещё }; oReq.send(); 

Sending:

 var oReq = new XMLHttpRequest(); oReq.open("POST", url, true); oReq.onload = function (oEvent) { // отправка завершена }; oReq.send(blob); 
  • Thanks I'll know. However, the problem is, the site uses auntefikatsii, and without it the image can not be reached. From the img tag you can not pull the image? - Filipp Mustang
  • @FilippMustang do not see how it interferes. The request is made through the current browser session. - D-side