将八位字节流转换为图像 [英] Convert octet-stream to image

查看:123
本文介绍了将八位字节流转换为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用$ http.get获取文件。
数据返回为application / octet-stream
但是我知道这个文件是图像。

I need to fetch a file using a $http.get. The data returns as application/octet-stream But i know this file is an image.

如何将此文件显示为图像?

How can i display this file as an image?

我试过

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

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

我得到200ok但图片没有显示

I get 200ok but the image isn't showing

是否可以将八位字节流转换为jpeg / png?

Is it possible to transform octet-stream to jpeg/png?

推荐答案

你可以使用 XMLHttpRequest() Blob URL.createObjectURL()从响应中创建blob URL

You can use XMLHttpRequest() to request image as Blob, URL.createObjectURL() to create a blob URL from 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();

这篇关于将八位字节流转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆