Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open the newly created image in a new tab?

Below code creates the image in the bottom of the same page. How to display that image into a new tab/window instead of displaying in the same page?

success: function (data) {             var image = new Image();             image.src = "data:image/jpg;base64," + data.d;             document.body.appendChild(image);         } 
like image 214
John Stephen Avatar asked Jan 06 '15 11:01

John Stephen


People also ask

How do I make an image open in a new tab?

Sometimes we may want the linked page to open in a new browser tab or window. To do this we add target="_blank" in the first section before > . We can do this for both an image or text link. That's it – how to add a text link, image and an image link in HTML.

How do I open a picture in a new page?

Note that you can also middle-click "View Image" apart from Ctrl + left-click to open the image in as new tab. Note that you can also middle-click "View Image" apart from Ctrl + left-click to open the image in as new tab.

What is the window method to open a new tab?

The open() method opens a new browser window, or a new tab, depending on your browser settings and the parameter values.


2 Answers

something like:

success: function (data) {         var image = new Image();         image.src = "data:image/jpg;base64," + data.d;          var w = window.open("");         w.document.write(image.outerHTML);     } 
like image 148
Super Babaca Avatar answered Sep 30 '22 06:09

Super Babaca


Current suggestions were not working on chrome, I was always getting a white page, now I am using

const base64ImageData = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='; const contentType = 'image/png';  const byteCharacters = atob(base64ImageData.substr(`data:${contentType};base64,`.length)); const byteArrays = [];  for (let offset = 0; offset < byteCharacters.length; offset += 1024) {     const slice = byteCharacters.slice(offset, offset + 1024);      const byteNumbers = new Array(slice.length);     for (let i = 0; i < slice.length; i++) {         byteNumbers[i] = slice.charCodeAt(i);     }      const byteArray = new Uint8Array(byteNumbers);      byteArrays.push(byteArray); } const blob = new Blob(byteArrays, {type: contentType}); const blobUrl = URL.createObjectURL(blob);  window.open(blobUrl, '_blank'); 

Thanks to Jeremy!
https://stackoverflow.com/a/16245768/8270748

like image 39
crazyx13th Avatar answered Sep 30 '22 08:09

crazyx13th