在vue.js中显示原始图像内容类型

TPP*_*PPZ 3 javascript content-type vue.js imagejpeg

我正在通过带有请求正文的HTTP GET从REST API中检索图像。

我设法通过测试使用node.js和检查了返回的内容chai.js

      expect(res).to.have.header('Content-Type', 'image/jpeg');
      expect(res).to.have.header('Access-Control-Allow-Origin', '*');
      expect(res).to.have.header('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers, Origin, X-Requested-With, Content-Type, Accept, Authorization');
      expect(res).to.have.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS, HEAD');
      expect(res).to.have.status(200);
      expect(res.body).to.be.instanceof(Buffer); // the image content
Run Code Online (Sandbox Code Playgroud)

vue.js文件中,我习惯将图像附加到<img ...>HTML标记上,如下所示:

<img v-bind:src="urlImg">
Run Code Online (Sandbox Code Playgroud)

然后在javascript部分中指定如下网址:

# this string representing the URL is returned by a function
this.urlImg = 'http://example.com/my_img.jpeg' 
Run Code Online (Sandbox Code Playgroud)

但是在这种情况下,我无法提供URL,因为HTTP GET希望正文返回具有内容类型的图像image/jpeg

我什至不确定这是否可行,而且我可能误会了内容类型image/jpeg应该如何工作。我该怎么做vue.js?有可能吗?有没有一种方法可以检查此HTTP响应的图像内容,就像Postman(Chrome应用程序)之类的东西一样,我无法检查将其伪装为text / Json的响应。

编辑

关于已接受的答案:最近提出的解决方案(UPDATE 2)为我工作(使用HTTP POST为请求提供JSON正文)。确保使用axioshttps://github.com/axios/axios)执行HTTP请求(您可以将其导入<script>Vue文件的一部分中,如下所示:)import axios from "axios";

我在使用vue-resourcehttps://github.com/pagekit/vue-resource)假装与相同axios,但事实并非如此,我花了一些时间才意识到。

Max*_*nev 5

如果已经拥有Buffer图像,则可以在客户端应用程序中指定预定义的链接:

this.urlImg = '/my/url/to/get/dynamic/image';
Run Code Online (Sandbox Code Playgroud)

并定义从服务器向客户端发送图像的路由(对于Express):

server.get("my/url/to/get/dynamic/image", function(req, res) {
   var myBuffer = yourFunctionReturnsBuffer();
   res.writeHead(200, {
    'Content-Type': 'image/jpeg',
    'Content-Length': myBuffer.length
   });
   res.end(myBuffer); 
 });
Run Code Online (Sandbox Code Playgroud)

Express + request的工作示例:My Gist

更新

通过ajax在浏览器中加载图像(下面的示例)。但是不可能使用本地浏览器XMLHttpRequest对象发送GET方法的请求主体(这是所有ajax库的基础)。从MDN:

send()接受一个可选参数,该参数使您可以指定请求的正文;这主要用于请求(例如PUT)。如果请求方法是GETHEAD忽略 body参数,并将请求正文设置为null。

this.urlImg = '/my/url/to/get/dynamic/image';
Run Code Online (Sandbox Code Playgroud)
server.get("my/url/to/get/dynamic/image", function(req, res) {
   var myBuffer = yourFunctionReturnsBuffer();
   res.writeHead(200, {
    'Content-Type': 'image/jpeg',
    'Content-Length': myBuffer.length
   });
   res.end(myBuffer); 
 });
Run Code Online (Sandbox Code Playgroud)

更新2

将图像解码为数组缓冲区

var app = new Vue({
    el: "#app",
    data: {
        // empty image
        src: "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
    },
    created() {
        let config = {
            // example url
            url: 'https://www.gravatar.com/avatar/7ad5ab35f81ff2a439baf00829ee6a23',
            method: 'GET',
            responseType: 'blob'
        }
        axios(config)
          .then((response) => {
              let reader = new FileReader();
              reader.readAsDataURL(response.data); 
              reader.onload = () => {
                  this.src = reader.result;
              }
          });
    }
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<div id="app">
  <img :src="src" alt="">
</div>
Run Code Online (Sandbox Code Playgroud)