SoFunction
Updated on 2025-04-04

How to preview binary images from the backend with vue

vue preview binary pictures from the backend

1. Create a new dialog box

Used to display pictures

<el-dialog title="" :="imgdialog"  width="70%">
  <img :src="src" alt=""/>
</el-dialog>

2. Download method

Replace request with axios

download(row) {
      this.$request({
        url: baseUrl + '/file/download',
        method: 'post',
        data: row,
        responseType: 'arraybuffer'  //This response type must be written      }).then(res =&gt; {
        // (res) // The binary file is printed out as garbled          = 'data:image/jpeg;base64,' + this.arrayBufferToBase64(res)
          = true
      }).catch(err =&gt; {
        (err)
      })
    },

3. Transcoding method

    arrayBufferToBase64(buffer) {
      var binary = ''
      var bytes = new Uint8Array(buffer)
      var len = 
      for (var i = 0; i < len; i++) {
        binary += (bytes[i])
      }
      return (binary)
    }

vue front-end to implement file preview function

The front end of the preview file

Omit the code that triggers the showdialogview() method by clicking the button

The first method

First add an input box to the template and bind a variable of string type

Write showdialogview() method

Call the background method. If the returned data is not null (the data returned by the background is returned by row by line), use a for loop and add it to the variable.

&lt;template&gt;
  &lt;div class="content"&gt;
    &lt;!--File preview--&gt;
    &lt;el-dialog title="Data Preview" :="dialogvisibleview" &gt;
      &lt;el-input type="textarea" :rows="10" placeholder="" v-model="dataview"&gt;
      &lt;/el-input&gt;
    &lt;/el-dialog&gt;
  &lt;/div&gt;
&lt;/template&gt;
&lt;script&gt;
  import api from '../../api/api'
  import $ from 'jquery';
  import axios from 'axios';
  export default {
    name: "DataFile",
    inject: ['reload'],
    data(){
      return {
        dialogvisibleview: false, // File preview        dataview: ''
      }
    },
    methods:{
      //Preview file      showdialogview(id) {
         ='';
        var dataView = new FormData;
       // var num=20;
        ("id",id);
        (dataView).then((response) =&gt; ())
          .then((data) =&gt; {
            if(data!=null){
               = !;
              //alert([0]);
              for (var i=0;i&lt;=-1;i++){
                +=[i]+"\n";
              }
            }
          });
      }
    },
    created: function () {
      ('cteated hook executes...');
    }
  }
&lt;/script&gt;

The second method

You can add a table tag to the dialog box and use v-html to bind variables in the tbody tag, so that the tags in the string can be displayed.

Because the data returned by the interface given by the background is a row of data corresponding to a subscript in the array, the data of each row is split into a string array and then placed in a cell. This shows it neatly and you can also add styles.

&lt;template&gt;
  &lt;div class="content"&gt;
    &lt;!--Preview modal box--&gt;
    &lt;el-dialog title="File Content Preview" :="previewVisible" modal-append-to-body="false" append-to-body="true" style="height: 100%;"width="80%"&gt;
      &lt;p style="color:red"&gt;If visualized,Please select the numeric column&lt;/p&gt;
      &lt;div  style="height: 600px;overflow:scroll;overflow-x:auto;overflow-y:auto "&gt;
        &lt;table  style="border-collapse:collapse;height:60px;"&gt;
          &lt;tbody  v-html="datafileView"&gt;&lt;/tbody&gt;&lt;/table&gt;
      &lt;/div&gt;
    &lt;/el-dialog&gt;
  &lt;/div&gt;
&lt;/template&gt;
&lt;script&gt;
  import api from '../../api/api'
  import $ from 'jquery';
  import axios from 'axios';
  export default {
    name: "DataFile",
    inject: ['reload'],
    data(){
      return {
         previewVisible:false,  // Preview modal box         datafileView:'', // Preview form      }
    },
    methods:{
      // Open the preview modal box      openPreview (id){
         = true;
        (id);
      },
      // Query preview in the background      viewData (fid) {
        ="";
        var dataView = new FormData;
        ("id",fid);
        (dataView).then((response) =&gt; ())
          .then((data) =&gt; {
            var viewDataString="&lt;tr&gt;";
            var dataArray = ((String)([0])).split(",");
            for(var i =0;i&lt;;i++){
              viewDataString=viewDataString+"&lt;td style='text-align:center;width:10%'&gt;&lt;input type='radio' name='tableheader' value='"+i+"' @click='f1()' /&gt;&lt;/td&gt;";
            }
            viewDataString = viewDataString+"&lt;/tr&gt;&lt;tr style='border-color:#1eb8f7;border-bottom-style:solid;border-width:1px'&gt;";
            for(var i =0;i&lt;;i++){
              viewDataString = viewDataString+"&lt;th style='text-align:center;width:10%'&gt;"+dataArray[i]+"&lt;/th&gt;";
            }
            viewDataString = viewDataString+"&lt;/tr&gt;";
            ();
            for(var x=1;x&lt;;x++){
              var dataArray = ((String)([x])).split(",");
              viewDataString = viewDataString+"&lt;tr&gt;";
              for(var i =0;i&lt;;i++){
                viewDataString = viewDataString+"&lt;td style='text-align:center;width:15%'&gt;"+dataArray[i]+"&lt;/td&gt;";
              }
              viewDataString = viewDataString+"&lt;/tr&gt;";
            }
             =viewDataString;
            ( );
          });
      }
    },
    created: function () {
      ('cteated hook executes...');
    }
  }
&lt;/script&gt;

Disadvantages: Using v-html, even if the tag is displayed in the browser, the methods inside cannot be used, and the methods inside are invalid.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.