Node.js将base64字符串作为表单数据发布 [英] Nodejs post base64 string as form data

查看:84
本文介绍了Node.js将base64字符串作为表单数据发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在接收一个编码为base64字符串的文件,作为json负载的一部分:

I am currently receiving a file encoded as a base64 string as part of a json payload:

{
    "file":"PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPGRlZmluaXRpb25zIHhtb..."
}

使用该字符串,我应该将文件作为multipart/form-data发布到其他服务,所以我有一个这样的方法(使用请求模块):

With that string I am supposed to post the file as multipart/form-data to a different service so I have a method like this (using request module):

var request = require('request');
var fs = require('fs');

var importFile = function(fileBase64Encoded, cb) {
    var decodedFile = new Buffer(fileBase64Encoded, 'base64');
    var r = request.post('http://localhost:8888/upload', function (err, httpResponse, body) {
        if (err) {
            cb(err);
        }
        cb(null, body);
    });
    var form = r.form();
    form.append('file', decodedFile);
}

这当前不起作用. 如果我将文件写入磁盘并像这样从那里读取文件:

And this is currently not working. If I write file to disk and read it from there like this:

var request = require('request');
var fs = require('fs');

var importFile function(fileBase64Encoded, cb) {

    var decodedFile = new Buffer(fileBase64Encoded, 'base64');
    fs.writeFile('temp.txt', decodedFile, function (err) {
        if (err) return console.log(err);
        var r = request.post('http://localhost:8888/upload', function (err, httpResponse, body) {
            if (err) {
                cb(err);
            }
            cb(null, body);
        })
        var form = r.form();
        form.append('file', fs.createReadStream('temp.txt'));
    });

}

然后它起作用了...所以有没有一种真正的方法将base64字符串作为有效参数传递给表单? (现在尝试使用缓冲区,但无法正常工作)

Then it works...so Is there a real way to pass the base64 string as a valid parameter to the form? (right now trying with a buffer and not working)

推荐答案

我假设http://localhost:8888/upload期望file是...文件".当您将文件流传递给form.append()时,它已经知道它是文件".传递Buffer时不这样做.

I assume that http://localhost:8888/upload is expecting file to be ... a "file". When you pass a file stream to form.append() it already knows it's a "file". When passing a Buffer it does not.

但是,可以通过将options对象作为第三个参数传递,来告诉form.append()Buffer解释为文件". options对象应具有一个名为filename的键,其中包含带有文件名的字符串. (可选)该对象还可以包含contentType字符串和knownLength整数.如果不包含contentType,则form.append()会尝试从filename派生内容类型.

You can, however, tell form.append() to interpret your Buffer as a "file" by passing an options object as the third argument. The options object should have a key called filename containing a string with the name of the file. Optionally, the object can also include a contentType string and knownLength integer. If contentType is not included, form.append() will try to derive the content-type from the filename.

除了确定contentType(未在options参数中显式传递它)以外,您指定的filename无关紧要(即,您可以使用所需的任何文件名).当然,除非您打算在服务器端代码上使用filename.

Other than to determine contentType (when it is not explicitly passed in the options argument), the filename you specify is irrelevant (i.e. you can use whatever file name you want). Unless, of course, you intend to use the filename on the server-side code.

在您的情况下,以下方法应该起作用.

In your case, the following should work.

var importFile = function(fileBase64Encoded, cb) {
    var decodedFile = new Buffer(fileBase64Encoded, 'base64');
    var r = request.post('http://localhost:8888/upload', function (err, httpResponse, body) {
        if (err) {
            cb(err);
        }
        cb(null, body);
    });
    var form = r.form();
    form.append('file', decodedFile, { filename: 'temp.txt' });
}

这篇关于Node.js将base64字符串作为表单数据发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆