Upload a file to Nodejs using python request

帅比萌擦擦* 提交于 2019-12-12 03:34:18

问题


I got some help here on the nodejs side: Extract file from POST request nodejs

I am new to node, and I am having problems getting this file node.js (Server PC)

var express = require('express');
var bodyParser = require('body-parser');
var multer = require('multer')

var app = express();
var upload = multer({ dest: 'uploads/' })

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));

/* POST commands */
app.post('/upload', upload.array(), function (req, res, next) {
console.log("I AM HERE");
})

python script (Client PC)

files = {'file': ('test_file', open(filePath, 'rb'))}
r = requests.post("http://192.168.2.39:3000/upload", files=files)

What am I doing wrong here? thanks

I am getting error 500,

Unexpected field

Error: Unexpected field
    at makeError (C:\qa\Automation\MyProj\Python Automation\Utilities\node\MyProjrtu\node_modules\multer\lib\make-error.js:12:13)
    at wrappedFileFilter (C:\qa\Automation\MyProj\Python Automation\Utilities\node\MyProjrtu\node_modules\multer\index.js:40:19)
    at Busboy.<anonymous> (C:\qa\Automation\MyProj\Python Automation\Utilities\node\MyProjrtu\node_modules\multer\lib\make-middleware.js:114:7)
    at emitMany (events.js:127:13)
    at Busboy.emit (events.js:201:7)
    at Busboy.emit (C:\qa\Automation\MyProj\Python Automation\Utilities\node\MyProjrtu\node_modules\busboy\lib\main.js:38:33)
    at PartStream.<anonymous> (C:\qa\Automation\MyProj\Python Automation\Utilities\node\MyProjrtu\node_modules\busboy\lib\types\multipart.js:213:13)
    at emitOne (events.js:96:13)
    at PartStream.emit (events.js:188:7)
    at HeaderParser.<anonymous> (C:\qa\Automation\MyProj\Python Automation\Utilities\node\MyProjrtu\node_modules\dicer\lib\Dicer.js:51:16)

回答1:


in node.js change upload.array() to upload.array('file')

and in python :

files = {'file': ('test_file', open(filePath, 'rb'))} to

files = {'file':open(filepath,'rb')}

check answer from this post Get image sent from post in node.js



来源:https://stackoverflow.com/questions/43266317/upload-a-file-to-nodejs-using-python-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!