I'm trying to display images hosted in the public directory of my rails app on a different website. I basically don't want a user to be able to type the src of the image tag into a browser and access the uri for my app where the image can be found.
After doing some digging, I determined that the way to achieve this may be to send the image as base64 encoded data, and then rebuild the image from that data on the website receiving the ajax response. Please let me know if I am mistaken.
I've successfully sent the base64 data from my rails app to my website through ajax, but I'm having difficulty building an image from the data.
This is my rails code:
def send_image(blank=nil)
require 'base64'
@image= "#{Rails.root}/public/test.jpg"
@imageEncoded = Base64.encode64(@image)
send_data @imageEncoded, :type => 'text/plain', disposition: "inline", :x_sendfile=> true
end
This is the ajax response:
L2FwcC9wdWJsaWMvcHVwcHkuanBn
This is the code to build the image from the ajax response:
$("#picture").attr("src","data:image/jpg;base64," + data);
I think I may be misunderstanding how this all works. When I get back the endcoded data and insert it into the src, an image is not displayed. It just shows a broken image icon.
If I send just the image file by itself (without encoding the data), the picture shows up in the ajax response when I look in the Network tab of the developer tools, but I cannot use that data to build an image. The data response looks like this:
���� :ExifMM* � s � � � ( 1 � 2 ҇i � -��' -��' Adobe Photoshop CS5 Macintosh2015:05:20 15:25:20 � 0221� ��� �� � n v ( ~ �H H ����Adobe_CM �� Adobed� ��� �� x� " �� �� ? 3 ! 1 AQa "q�2 ���B#$ R�b34r��C %�S���cs5 ���&D�TdE£t6 �U�e���u��F'���������������Vfv��������7GWgw�������� 5 !1 AQaq" 2�� ��B#�R��3$b�r��CS cs4�% ��� &5��D�T� dEU6te����u��F���������������Vfv��������'7GWgw������� ?�T�I%)$�IJI$�R�I$���I%)$�IJI$�R�I$���I%)$�IO���T�I%)$�IJI #&� .�dž�J��ɶ��0?ю Ys� o.̘�J~ �z�Ugk �Ø� iU~u�: ��* >� � hL�^� HЎ$*Y9��;��ѵ ��_7I���� �� [� � � � � HI ��K�D�� c ӈ�uQ� � a��;C���;\�} s V%e��)��lm���K r_�-�\��i�IT�/�sa�A�Q�z��1*�r�@ wk�H ��!�� ~��� �O�DN ���7 I$�B�I$�����T�I%) /)��op�th�(� ���2KX k=�?�Qg�����@Ɇ s�� �� k��* u�%���\�W��Oc��� ϱ�Ǭ�y��=�� i?�z1�� A�+�<��Vvo� Q��Z� / �c� �����Of�+�5�hy2����; � ���:Ů=c���W]m kˁ��m{v��Ќx�t�e d[���7# ��6ڈp�S � � �Nr�!� N��X8�{�w=�p ��#��+��u�>Ө�S���� ��n����i� � �� B�C�5�'� y ��t�t��� kH �.�^�'� ���/ � ������X���-��-�i>+3�P�.nѸ q� � ��T*$�uu��{���;Lhc�7} ��ަ����X���㸏 �n�_K�ߣi�q �
Can someone let me know where I might be going wrong here? Do I have to send the image, and then base64 encode it and add it to the src of the image tag? Am I failing to understand how encoding works?
RESPONSE TO COMMENTS
So the code that I would have to write looks like this?
var request = new XMLHttpRequest();
request.responseType = "blob";
request.open("GET", railsAppEndpoint);
*This is where I'm unclear as to how to proceed based on your comment. I've never used fetch before. I looked up some documentation on fetch and it looks as though the first argument that you are supposed to pass into fetch is a url. Are you saying that I have to open up an XMLHttpRequest, and then also use fetch? Or is fetch supposed to replace the XMLHttpRequest.
I'm just guessing at the correct syntax here:
fetch()
.then(
response => response.blob()
)
*Something seems missing here.
**At this point I don't get how to obtain the Blob url. I know how to set the src of an image, but I need to complete the other steps first.