简体   繁体   English

base64编码图像主机URL或服务器文件路径

[英]base64 encode image host url or server file path

When I encode image data to a base64 string I use the server file path to get the image data with fs.readFile() . 当我将图像数据编码为base64字符串时,我使用服务器文件路径通过fs.readFile()获取图像数据。 I have question: does this mean other people can decode the base64 string then get the server path from the encoded data as below? 我有一个问题:这是否意味着其他人可以解码base64字符串,然后从编码数据中获取服务器路径,如下所示?

...
fs.readFile(destinationFilePath, function(error, data){
  fulfill(data.toString('base64'));
});

I don't want to leak my server path to so I also tried encode the host url like below code, I'm not sure this correct way to use base64? 我不想泄漏我的服务器路径,因此我也尝试对主机url进行编码,如以下代码所示,我不确定使用base64的正确方法吗? and I don't get any error but also I got no response - did I miss something? 而且我没有任何错误,但是我也没有回应-我错过了什么吗?

var base64EncodeData = function(destinationFilePath) {
  return new Promise(function (fulfill, reject){
      var request = require('request').defaults({ encoding: null });
      request.get(destinationFilePath, function (error, response, body) {
        if (!error && response.statusCode == 200) {
          data = "data:" + response.headers["content-type"] + ";base64," + new Buffer(body).toString('base64');
          console.log(data);
          fulfill(data);
        }
      });
  });
};

No you don't leak your server path by base64 encoding images. 不,您不会通过base64编码图像泄漏服务器路径。 The base64 you are generating only includes a base64 representation of the binary image data. 您生成的base64仅包含二进制图像数据的base64表示形式。 Indeed by base64 encoding them you remove any use of a path when you display them within a HTML page for example: 实际上,通过对它们进行base64编码,您可以在HTML页面中显示它们时删除对路径的任何使用,例如:

<img alt="base64 image" src="data:image/png;base64,isdRw0KGgot5AAANdSsDIA..." />

The src attribute contains a flag that data is being provided data: the file mimetype image/png; src属性包含一个标志,指示正在向data:提供data:文件mimetype image/png; the encoding base64, and the encoded image data isdRw0KGgot5AAANdSsDIA... . 编码base64,编码的图像数据为isdRw0KGgot5AAANdSsDIA...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM