反应dropzone和nodejs文件上传

时间:2017-07-29 09:27:38

标签: node.js reactjs react-dropzone

我正在使用react-dropzone上传文件。来自react-dropzone,我正在预览这是一个blob网址。

  

预览:" blob:http://localhost:8080/96ff7bfc-2825-4316-8914-943a1b3b7f28"

如何在服务器端nodejs中访问相同的内容?我想在服务器中获得该文件的副本,并将服务器路径返回给客户端。

1 个答案:

答案 0 :(得分:-1)

nodejs代码:请参阅link

var express  =  require( 'express' );
var multer   =  require( 'multer' );
var upload   =  multer( { dest: 'uploads/' } );
var sizeOf   =  require( 'image-size' );
var exphbs   =  require( 'express-handlebars' );
require( 'string.prototype.startswith' );

var app = express();

app.use( express.static( __dirname + '/bower_components' ) );

app.engine( '.hbs', exphbs( { extname: '.hbs' } ) );
app.set('view engine', '.hbs');

app.get( '/', function( req, res, next ){
  return res.render( 'index' );
});

app.post( '/upload', upload.single( 'file' ), function( req, res, next ) {

  if ( !req.file.mimetype.startsWith( 'image/' ) ) {
    return res.status( 422 ).json( {
      error : 'The uploaded file must be an image'
    } );
  }

  var dimensions = sizeOf( req.file.path );

  if ( ( dimensions.width < 640 ) || ( dimensions.height < 480 ) ) {
    return res.status( 422 ).json( {
      error : 'The image must be at least 640 x 480px'
    } );
  }

  return res.status( 200 ).send( req.file );
});

app.listen( 8080, function() {
  console.log( 'Express server listening on port 8080' );
});