简体   繁体   English

如何使用feathers.js和multer上传图片?

[英]How to upload a picture using feathers.js and multer?

I'm working with feathers.js on the back-end and React on the front end and I need to implement a way to upload a picture. 我正在使用后端的feathers.js和前端的React,我需要实现一种上传图片的方法。 I'm using multer to handle the upload (and I've tried using busboy as well), but I can't seem to get the actual picture uploaded, or at least access it on req.file . 我正在使用multer来处理上传(我也试过使用busboy),但我似乎无法上传实际图片,或者至少在req.file上访问它。

On the client side I have: 在客户端我有:

<form method="post" enctype="multipart/form-data" action="/picture/upload">
  <input type="file" name="avatar" />
  <input type="submit" value="Submit" />
</form>

In /src/middleware/index.js I have: /src/middleware/index.js我有:

'use strict';

const uploadPicture = require('./uploadPicture');

const handler = require('feathers-errors/handler');
const notFound = require('./not-found-handler');
const logger = require('./logger');
var multer = require('multer');
const upload = multer({ dest: '../../client/img'});

module.exports = function() {
  // Add your custom middleware here. Remember, that
  // just like Express the order matters, so error
  // handling middleware should go last.

  const app = this;

  app.post('/picture/upload', upload.single('avatar'), uploadPicture(app));
  app.use(notFound());
  app.use(logger(app));
  app.use(handler());
};

This is src/middleware/uploadPicture.js : 这是src/middleware/uploadPicture.js

'use strict';

module.exports = function(app) {
  return function(req, res, next) {
    console.log('req.file', req.file);
    console.log('req.body', req.body);

  };
};

req.file is always undefined, and req.body does contain the name of the image I uploaded. req.file始终未定义, req.body确实包含我上传的图像的名称。

I have tried using mutler and busboy on a basic Express project for testing, and it works perfectly, so that makes me think that maybe it has something to do with the middleware feathers.js uses and probably it changes some header or something, so multer can't append the file to the request object. 我已经尝试在一个基本的Express项目上使用mutler和busboy进行测试,并且它工作得很好,所以这让我觉得它可能与中间件feat.js有关,并且可能它改变了一些标题或者什么,所以multer无法将文件附加到请求对象。

This is the order in which middleware is defined in src/app.js , which is where the server instance is run: 这是在src/app.js定义中间件的顺序,这是运行服务器实例的位置:

app.use(compress())
  .options('*', cors())
  .use(cors())
  .use('/', serveStatic( app.get('client') ))
  .use(bodyParser.json())
  .use(bodyParser.urlencoded({ extended: true }))
  .configure(hooks())
  .configure(rest())
  .configure(services)
  .configure(middleware);

Any thoughts on how to handle the image upload in this scenario? 有关如何在这种情况下处理图像上传的任何想法?

I am using react, so enctype is not a supported HTML attribute . 我正在使用react,因此enctype不是受支持的HTML属性 I should have used encType in my form. 我应该在我的表单中使用encType That fixed the problem. 这解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM