简体   繁体   English

如何从 Node.js 中的 base64 字符串发送表单数据中的文件?

[英]How to send files in a formdata from their base64 string in Node.js?

I'm currently working on a Node.js middleware and I need to send a file to an API using a formdata.我目前正在研究 Node.js 中间件,我需要使用 formdata 将文件发送到 API。

I've managed to do this when I use a file on my local storage using fs to create a ReadStream and put it in my formdata.当我使用 fs 使用本地存储上的文件创建 ReadStream 并将其放入我的表单数据时,我设法做到了这一点。

I use request-promise to send my data:我使用 request-promise 发送我的数据:

var formData = {
  'file': fs.createReadStream('myfile.jpg')
}

var options = {
  method: method,
  uri: url,
  formData: formData
}

return rp(options).then(response => response)

Now I don't fully understand what is going on under the hood.现在我不完全了解引擎盖下发生了什么。

I need to send a file which I only have in a base64 string, and the API is waiting for a file just like I sent "myfile.jpg".我需要发送一个只有 base64 字符串的文件,并且 API 正在等待一个文件,就像我发送的“myfile.jpg”一样。

I've tried to send a buffer built from the base64 string:我试图发送一个从 base64 字符串构建的缓冲区:

var buffer = Buffer.from(base64string, 'base64')

var formData = {
  'file': buffer
}

But it didn't work.但它没有用。 I could eventually create a binary file from my Buffer and use fs to create a stream from it but I hope there is a better way to achieve this.我最终可以从我的缓冲区创建一个二进制文件并使用 fs 从中创建一个流,但我希望有更好的方法来实现这一点。

Also I'm not confortable working with things I don't understand so if you have any informations on how file streams work with formdatas, I'll take it!另外,我不喜欢处理我不明白的事情,所以如果您有关于文件流如何与 formdatas 一起工作的任何信息,我会接受的!

Here is an example using Axios and FormData这是使用 Axios 和 FormData 的示例

const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');

// Create a new form instance
const form = new FormData();

const file =  fs.readFileSync('./example.jpg');
// or/and
const fileStream = fs.createReadStream('./example.jpg');

// For base64

const buffer = Buffer.from(YOUR_BASE64_STRING, 'base64');

// Create a form and append image with additional fields

form.append('file', file, 'example.jpg');
// or/and
form.append('fileStream', fileStream, 'example.jpg');
// or/and
form.append('buffer', buffer, 'example.jpg');

// Send form data with axios
const response = await axios.post('https://example.com', form, {
    headers: {
        ...form.getHeaders(),
        // some other headers if needed
    },
});

I am not sure if this will work in your setting, but what I have done is create a buffer from the data and then append a "path" property. 我不确定这是否适用于您的设置,但我所做的是从数据创建缓冲区,然后附加“路径”属性。 This property allows request or some other library to guess the type of the data: 此属性允许请求或其他库猜测数据的类型:

  var istream = new Buffer(image, 'base64');
  istream.path = "pic.png";
  server.post( "/upload", {"file":istream}, function(...){} )

The upload worked flawlessly. 上传工作完美无瑕。 This depends on your http layer (I guess). 这取决于你的http层(我猜)。

const buffer = Buffer.from(fileBase64Encoded, 'base64');
const form = new FormData();
form.append('image', buffer, fileName);
const headers = {headers: {'Content-Type':`multipart/form-data; boundary=${form.getBoundary()}`}};
return server.post(`${process.env.SERVER}/upload`, form, headers);

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

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