js中的帖子发送的图像

js中的帖子发送的图像

本文介绍了获取从node.js中的帖子发送的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我需要使用python通过帖子发送图像,然后将其下载到node.js服务器端.

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

Python代码:

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代码:

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

但是我似乎无法在服务器上正确下载文件,因此我想知道python用于打开图像的格式以及如何修复node.js代码以使其能够正确下载图像.

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.

代码中存在一些问题,我现在尝试使用multer,但似乎无法使其正常工作.

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

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代码:

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

我尝试了upload.single和upload.array,但都没有用.

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

推荐答案

所以我终于用multer弄清楚了...错误地命名了密钥,这就是为什么我不能正确使用multer的原因.

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

这篇关于获取从node.js中的帖子发送的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 16:15