nodejs multer获取req.file但没有将其上传到目标

时间:2018-03-22 14:16:07

标签: node.js file express upload multer

multer.js

//handle createPartner route
router.post("/partner/new", upload.single('image'), function(req, res){

  console.log(req.file);
  console.log(req.body);

  req.body.Partner["image"] ="/static/imgs/"+req.file.filename;

  req.body.Partner.body = req.sanitize(req.body.Partner.body);

  Partner.create(req.body.Partner, function(err, createdPartner){
      if(err){
          console.log(err);
          res.redirect("/admin/partner/new");
      }else{
          res.redirect("/partners");
      }
  });
});

router.js

{ fieldname: 'image',
  originalname: 'fb.png',
  encoding: '7bit',
  mimetype: 'image/png',
  destination: '../public/imgs/',
  filename: 'image-1521727739861.png',
  path: '../public/imgs/image-1521727739861.png',
  size: 21532 }
{ Partner: { name: 'test', bio: 'test', image: '', website: 'test' } }

我得到以下console.log

 using System;
 using Android.Content;
 using MyGlobe.Droid.Renderers;
 using Xamarin.Forms;
 using Xamarin.Forms.Platform.Android;
 using Android.Graphics;
 using Android.Graphics.Drawables;

 [assembly: ExportRenderer(typeof(Frame),typeof(MyGlobe.Droid.Renderers.BorderFrameRenderer))]
 namespace Yours.Droid.Renderers
 {
 public class BorderFrameRenderer : FrameRenderer
 {
    public override void Draw(Canvas canvas)
    {
        base.Draw(canvas);
        using (var strokePaint = new Paint())
        using (var rect = new RectF(0, 0, canvas.Width, canvas.Height))
        {
            string FrameBorderColorHex = String.Format("#{3:X2}{0:X2}{1:X2}{2:X2}", (int)(Element.OutlineColor.R * 255), (int)(Element.OutlineColor.G * 255), (int)(Element.OutlineColor.B * 255), (int)(Element.OutlineColor.A * 255));

            // stroke
            strokePaint.SetStyle(Paint.Style.Stroke);
            strokePaint.Color = Android.Graphics.Color.ParseColor(FrameBorderColorHex);
            strokePaint.StrokeWidth = 5;

            var radius = 10;

            canvas.DrawRoundRect(rect, radius, radius, strokePaint);  // stroke
        }
    }

    public BorderFrameRenderer(Context context) : base(context)
    {
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
    {
        base.OnElementChanged(e);
    }
}
}

所以multer会获取文件,但不会将其上传到目的地! / public和/ public / imgs的权限是777.我搜索到的任何地方都使用另一种方法进行multer但是这样我必须事先指定(single / multiple / any),这是不正确的。

1 个答案:

答案 0 :(得分:1)

您需要包含我在评论中提到的绝对路径。

您可以使用path.join轻松完成此操作,如下所示:

var path = require('path');
var dir = path.join(__dirname, '../public/imgs')

这将连接__dirname,这是当前文件与图像文件的绝对路径。