【问题标题】:Converting to base64 returns a very short string (too short)转换为 base64 会返回一个非常短的字符串(太短)
【发布时间】:2017-04-26 20:04:30
【问题描述】:

我提交的表单有时包含单个图像,有时包含多个图像。但是,当我提交图像时,比如说1.jpg,我的节点服务器的控制台输出是MS5qcGc=。我假设以某种方式没有发送图像,只有文本?如何发送整个图像,并将其转换为base64

客户

<form method="post">
    <div class="form-group">
        <label for="imgs">Images</label><input accept="image/*" multiple name="imgs" type="file">
    </div>
</form>

服务器

if (typeof req.body.imgs == "string") {
  console.log(new Buffer(req.body.imgs).toString('base64'));
} else {
  for ( i in req.body.imgs) {
    console.log(new Buffer(req.body.imgs[i]).toString('base64'));
  }
}

【问题讨论】:

  • 你能分享nodejs代码的其他部分吗,你是如何在中间件或访问req.body.imgs之前处理请求的。看起来您在 console.log 中只获得了文件名。
  • @Abhishek 我正在使用快递
  • 看起来 Sam Ax 的回答可以解决问题。

标签: javascript node.js forms


【解决方案1】:

您需要将值为multipart/form-dataenctype 属性添加到您的form 元素中。

<form method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label for="imgs">Images</label><input accept="image/*" multiple name="imgs" type="file">
    </div>
</form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-17
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2014-06-24
    • 2013-03-26
    相关资源
    最近更新 更多