18

Example:

<img src='https://example.com/getImage?q=img_ref' />

And the https://example.com/getImage requires us to send some specified headers to make the request valid. Is it possible, or do I have to use ajax for this?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Sherzod
  • 5,041
  • 10
  • 47
  • 66
  • @alex23: thanks! I was just wondering if it's possible with some hidden HTML5 feature. Seems like not. – Sherzod May 07 '13 at 19:19
  • 1
    Were you able go get this working? I have similar issue that I need to solve. – DrChanimal Jul 20 '13 at 18:56
  • 1
    @DrChanimal: no, it's not possible. I ended up using ajax since our back-end supported base64 responses. – Sherzod Aug 18 '13 at 06:41
  • 1
    @shershams after using ajax, how do you set it as the `src` attribute of the `img` tag? – mehulkar Mar 30 '15 at 19:03
  • 1
    @mehulkar: You would have to set an id/class to img tag, and then fill the src with base64 string with "data:image/png;base64," prefixed using Javascript – Sherzod Mar 30 '15 at 21:40
  • @shershams how do I convert the downloaded file to a base64 string? – mehulkar Apr 01 '15 at 08:00
  • @mehulkar: check this out: http://stackoverflow.com/questions/7370943/retrieving-binary-file-content-using-javascript-base64-encode-it-and-reverse-de – Sherzod Apr 01 '15 at 15:55
  • base64 option hinders performance by the way. If you have the chance, you can consider sending your data with querystring. – Kemal Taşkın Jun 03 '15 at 07:28
  • @mehulkar Re: _"after using ajax, how do you set it as the src attribute of the img tag?"_ I have implemented something similar, loading an image using ajax. Once my request has completed, I simply set the src of the image element to the URL using javascript and the browser (at least those I've tested) uses the image that is in cache. _[My purpose for using this technique was to provide a loading progress bar for the image, rather than the OP's requirement of sending specific headers.]_ – Sean Jan 05 '16 at 22:36
  • duplicate here https://stackoverflow.com/questions/27000152/set-custom-header-for-the-request-made-from-img-tag – Paulo Jun 11 '18 at 04:59

1 Answers1

11

That facility isn't available through HTML. I would make it as an ajax request.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445