gpt4 book ai didi

python - 在node.JS中使用POST上传两个文件

转载 作者:太空宇宙 更新时间:2023-11-04 02:14:22 24 4
gpt4 key购买 nike

我在 Python 中得到了以下代码:

import requests
fileslist = [('file[]',('user_query.txt', open('user_query.txt', 'rb'), 'text/plain')),
('file[]',('wheatData.csv', open('wheatData.csv', 'rb'), 'text/csv')),]

r = requests.post('url',
files=fileslist)

我正在尝试将其转换为 node.JS 版本。到目前为止我已经得到了这个:

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

var req = request.post(url, function (err, resp, body) {
if (err) {
console.log('Error!');
} else {
console.log(body);
}
});

var form = req.form();
form.append('wheatData.csv', fs.createReadStream('wheatData.csv'));
form.append('user_query.txt', fs.createReadStream('user_query.txt'));

我做错了什么?

最佳答案

这就是使用 express 执行此操作的方法 body-parser 模块来解析 post 请求并获取您需要的文件。这就是您的 Node.js 服务器中的内容。

导入所有模块:

var express = require("express");
var bodyParser = require('body-parser')
var app = express(); //init express app()
var util = require('util');

//APP CONFIGURATION >> Skip this if you dont want CORS
app.use(express.static('app')); // use this as resource directory
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});

配置帖子网址:

//url => url/for/mypostrequest
app.post(url, done, function (req, res) {

//Handle the post request body here...
var filesUploaded = 0;
//check if files present
if (Object.keys(req.files).length === 0) {
console.log('no files uploaded');
} else {
console.log(req.files);
var files = req.files.file1;
//If multiple files store in array..
if (!util.isArray(req.files.file1)) {
files = [req.files.file1];
}
filesUploaded = files.length;
}
res.json({message: 'Finished! Uploaded ' + filesUploaded + ' files. Route is /files1'});
});

确保所有模块均已安装并作为 package.json 中的依赖项出现

<小时/>

从 Node 进行 api post 调用的代码..

首先在您的服务器中包含 http 模块。

var http = require('http');
var querystring = require('querystring');
var fs = require('fs');

然后包含以下代码以从 Node 服务器发出发布请求

    var file1, file2;
//Read first File ...
fs.readFileSync('wheatData.csv', function (err, data) {
if (err) {
console.log('Error in file reading...');
}
file1 = data;

//Read second file....
fs.readFileSync('wheatData.csv', function (err, data) {
if (err) {
console.log('Error in file reading...');
}
file2 = data;

//Construct the post request data..
var postData = querystring.stringify({
'msg': 'Hello World!',
'file1': file1,
'file2': file2
});
var options = { //setup option for you request
hostname: 'www.path/to/api/',
port: 80,
path: '/upload',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
};
var req = http.request(options, function (res) {
console.log('STATUS:' + res.statusCode);
console.log('HEADERS:' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
res.on('end', function () {
console.log('No more data in response.');
});
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write(postData);
req.end();

});
});

请注意,代码尚未在实时服务器上进行测试,您可能需要根据您的配置进行更改。

您还可以使用其他库(例如 request 或 Needler..etc)从 Node 服务器进行后期调用,如 this 中的建议。发布。

关于python - 在node.JS中使用POST上传两个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36417662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com