admin管理员组

文章数量:1394585

I need to use python to send an image through post and then download it on the node.js server side.

Python code:

import requests
from PIL import Image
import json

url = 'http://127.0.0.1:8080/ay'
files = {'file': open('image.jpg', 'rb')}
r = requests.post(url, data = files)

Node.js code:

var app = express();
app.use(bodyparser.json({ limit: '50mb' }));
app.use(bodyparser.urlencoded({ limit: '50mb', extended: true }));

app.post('/ay', function(req, res) {
    var base64Data = req.body.file
    require("fs").writeFile("out.png", base64Data, 'base64', function(err) {
        console.log(err);
    });

    res.send('done');
});

But I can't seem to download the file properly on the server so I'm wondering what format python uses to open images and how I can fix the node.js code so that it can properly download the image.

Edit: there were a few issues with the code, I'm trying to use multer now but can't seem to get it working.

Python code:

import requests

url = 'http://127.0.0.1:8080/ay'
files = {'file': open('image.jpg', 'rb')}
r = requests.post(url, files = files)

Node.js code:

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

app.post('/ay', upload.single('avatar'), function (req, res, next) {
    console.log(req.file)
    res.send("done");
});

app.post('/ay', upload.array('photos', 12), function (req, res, next) {
    console.log(req.files)
    res.send("done");
});

I've tried both upload.single and upload.array but neither work.

I need to use python to send an image through post and then download it on the node.js server side.

Python code:

import requests
from PIL import Image
import json

url = 'http://127.0.0.1:8080/ay'
files = {'file': open('image.jpg', 'rb')}
r = requests.post(url, data = files)

Node.js code:

var app = express();
app.use(bodyparser.json({ limit: '50mb' }));
app.use(bodyparser.urlencoded({ limit: '50mb', extended: true }));

app.post('/ay', function(req, res) {
    var base64Data = req.body.file
    require("fs").writeFile("out.png", base64Data, 'base64', function(err) {
        console.log(err);
    });

    res.send('done');
});

But I can't seem to download the file properly on the server so I'm wondering what format python uses to open images and how I can fix the node.js code so that it can properly download the image.

Edit: there were a few issues with the code, I'm trying to use multer now but can't seem to get it working.

Python code:

import requests

url = 'http://127.0.0.1:8080/ay'
files = {'file': open('image.jpg', 'rb')}
r = requests.post(url, files = files)

Node.js code:

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

app.post('/ay', upload.single('avatar'), function (req, res, next) {
    console.log(req.file)
    res.send("done");
});

app.post('/ay', upload.array('photos', 12), function (req, res, next) {
    console.log(req.files)
    res.send("done");
});

I've tried both upload.single and upload.array but neither work.

Share Improve this question edited Mar 2, 2016 at 21:19 user1883614 asked Mar 2, 2016 at 18:31 user1883614user1883614 9653 gold badges17 silver badges31 bronze badges 4
  • Have you included middleware to handle the POST data in Node ? – adeneo Commented Mar 2, 2016 at 18:34
  • Yup! I just edited my code to show it. I am receiving the image on the server side but I don't know how it's encoded so I can't seem to decode it. – user1883614 Commented Mar 2, 2016 at 18:37
  • bodyParser does not work on files, it says so right on the top of the documentation. You need multer, busboy or one of the others that support files. Also, files has to be sent as multipart/form with the correct keys, not JSON. – adeneo Commented Mar 2, 2016 at 18:42
  • And I think your python should be r = requests.post(url, files=files) – adeneo Commented Mar 2, 2016 at 18:48
Add a ment  | 

2 Answers 2

Reset to default 3

So I finally figured it out using multer... incorrectly naming the key is why I couldn't use multer properly.

Python:

import requests
url = 'http://127.0.0.1:8080/ay'
files = {'file': open('image.jpg', 'rb')}
r = requests.post(url, files = files)

Node.js:

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

app.post('/ay', upload.array('file', 12), function (req, res, next) {
    console.log(req.files)
    res.send("done");
});

Have a look at this blog post which gives an example regarding how to access an uploaded file in node.js

In that example, after you load the bodyParser middleware, you have access to an object called req.files which contains the info regarding your uploaded file.

Do a console.log(req.files) and see what it displays.

The bodyParser middleware can be used to read uploaded files in Express v3, which is no longer maintained.

If you use v4 or above, you can use the connect-multiparty middleware, like this:

var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
app.post('/ay', multipartMiddleware, function(req, resp) {
  console.log(req.body, req.files);
  // don't forget to delete all req.files when done 
});

Also, I think your Python code is not uploading properly. Try with:

requests.post('http://127.0.0.1:8080/ay', files={'image.jpg': open('image.jpg', 'rb')})

本文标签: javascriptGet image sent from post in nodejsStack Overflow