Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在sails.js或服务器端JavaScript中将图像转换为base64编码的数据URL?_Javascript_Node.js_Sails.js - Fatal编程技术网

如何在sails.js或服务器端JavaScript中将图像转换为base64编码的数据URL?

如何在sails.js或服务器端JavaScript中将图像转换为base64编码的数据URL?,javascript,node.js,sails.js,Javascript,Node.js,Sails.js,我正在sails.js中制作一个小应用程序,我需要在数据库中存储图像。为此,我需要将图像转换为base64编码的数据URL,以便在我的sails模型中将其保存为字符串。但是,我不知道如何将其转换为这种形式。所有关于将图像转换为base64编码数据URL的老问题,他们都回答了关于在客户端进行转换的问题。但是,我希望在服务器端执行此操作,同时通过post请求获取图像。如何实现这一点?据我所知,您希望将文件转换为base64编码字符串。无论文件是否为图像,这都无关紧要 var fs = require

我正在
sails.js
中制作一个小应用程序,我需要在数据库中存储图像。为此,我需要将图像转换为base64编码的数据URL,以便在我的sails模型中将其保存为字符串。但是,我不知道如何将其转换为这种形式。所有关于将图像转换为base64编码数据URL的老问题,他们都回答了关于在客户端进行转换的问题。但是,我希望在服务器端执行此操作,同时通过post请求获取图像。如何实现这一点?

据我所知,您希望将文件转换为base64编码字符串。无论文件是否为图像,这都无关紧要

var fs = require('fs');

// function to encode file data to base64 encoded string
function base64_encode(file) {
    // read binary data
    var bitmap = fs.readFileSync(file);
    // convert binary data to base64 encoded string
    return new Buffer(bitmap).toString('base64');
}
用法:

var base64str = base64_encode('kitten.jpg');

这里有另一个简单的方法,在列出图像时使用它

@{
    if (item.ImageData != null)
    {
        string imageBase64 = Convert.ToBase64String(item.ImageData);
        string imageSrc = string.Format("data:image/gif;base64,{0}", imageBase64);
        <img src="@imageSrc" width="100" height="100" />
    }
}
@{
如果(item.ImageData!=null)
{
字符串imageBase64=Convert.tobase64字符串(item.ImageData);
string imageSrc=string.Format(“数据:image/gif;base64,{0}”,imageBase64);
}
}

可以使用readFileSync实现,将图像路径作为第一个参数传递,将编码选项作为第二个参数传递。如下所示:

var fs = require('fs');

var imageAsBase64 = fs.readFileSync('./your-image.png', 'base64');
根据文件:

fs.readFileSync(路径[,选项])

fs.readFile()的同步版本。返回 路径的内容

如果指定了编码选项,则此函数返回 一串否则它将返回一个缓冲区


//您可以使用image-to-base64

const imageToBase64 = require('image-to-base64');

imageToBase64("URL") // insert image url here. 
    .then( (response) => {
          console.log(response);  // the response will be the string base64.
      }
    )
    .catch(
        (error) => {
            console.log(error);
        }
    )

您的服务器端技术是什么?
fs.readFile
在未传递编码时提供缓冲区,因此无需使用
新缓冲区()。不要忘记添加数据URI前缀:
data:[]base64],
例如:
data:image/png;base64,
对于PNG图像。更多信息:新缓冲区()已弃用。您可以使用fs.readFileSync(文件,{encoding:'base64});base64 encoded only是免费的,但要创建dataURI,需要某种mime db。我认为这根本不是javascript,而是allexcuse me之后的Razor语法,这个问题是关于JS的,而不是C#或ASP.NET。这看起来像C#,问题是Javascript@JSON-C11为什么不使用此的异步版本?我试图在angular中使用它,但得到一个错误:找不到模块:错误:无法解析“fs”
const imageToBase64 = require('image-to-base64');

imageToBase64("URL") // insert image url here. 
    .then( (response) => {
          console.log(response);  // the response will be the string base64.
      }
    )
    .catch(
        (error) => {
            console.log(error);
        }
    )