Get image sent from post in node.js

流过昼夜 提交于 2020-01-02 06:32:20

问题


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.


回答1:


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");
});



回答2:


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')})


来源:https://stackoverflow.com/questions/35755333/get-image-sent-from-post-in-node-js

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