Convert octet stream to image

I need to get the file using $ http.get. The data is returned as an application / octet stream. But I know that this file is an image.

How can I display this file as an image?

I tried

var nuroImage = new Image();
nuroImage.onload = function {
  scope.imageSrc = this.src;
}

$http(req).then(
  funciton(response) {
    nuroImage.src = "data:application/octet-stream," + response.data
  }

I get 200ok, but the image is not displayed

Is it possible to convert octet stream to jpeg / png?

+4
source share
2 answers

You can use XMLHttpRequest()to request an image like Blob, URL.createObjectURL()to create a blob url from a response

var nuroImage = new Image;
var request = new XMLHttpRequest();
request.responseType = "blob";
request.onload = function() {
  nuroImage.src = URL.createObjectURL(this.response)
}
request.open("GET", "/path/to/image/file");
request.send();
+3
source

I think you need something like this:

  • Use atobto encode the resulting file of a compressed image of long length
  • <img src="data:image/png;base64,...."> html5

PNG, mime.

<img src="data:image/gif;base64,R0lGODlhEAAOALMAAOazToeHh0tLS/7LZv/0jvb29t/
f3//Ub//ge8WSLf/rhf/3kdbW1mxsbP//mf///yH5BAAAAAAALAAAAAAQAA4AAARe8L1Ekyky67
QZ1hLnjM5UUde0ECwLJoExKcppV0aCcGCmTIHEIUEqjgaORCMxIC6e0CcguWw6aFjsVMkkIr7g7
7ZKPJjPZqIyd7sJAgVGoEGv2xsBxqNgYPj/gAwXEQA7"/>
Hide result
+2

All Articles