multer未使用提供的默认示例上传。

时间:2018-09-29 02:28:12

标签: angular express npm multer

我在尝试使我的代码正常工作时遇到了问题,而从multer中得到的消息却没有。相信我,我检查了所有地方,请帮忙。

//All the require functions that make the app work
const express = require('express')
const nodemon = require('nodemon')
const http = require('http')
const path = require('path')
const fs = require('fs')
const mongoose = require('mongoose')
const bodyParser = require('body-parser')
const morgan = require('morgan')
const jwt = require('jsonwebtoken')
const config = require('./config/config.js')
const fileUpload = require('express-fileupload');
const https = require('https')
const mongoSanitize = require('express-mongo-sanitize');
var stripe = require("stripe")(config.stripeAPI);
var multer  = require('multer')
var upload = multer({dest: "./uploads"}).single('photo');



app.post('/upload', (req, res, next) => {
        var path = '';
        upload(req, res, function (err) {
                if (err) {
                // An error occurred when uploading
                console.log(err);
                return res.status(422).send("an Error occured")
                }  
        // No error occured.
                path = req.files.path;
                return res.send("Upload Completed for "+path); 
        });     
});

这实际上是我在示例中使用的

1 个答案:

答案 0 :(得分:0)

磁盘存储可能有问题。

let multer  = require('multer')

let storage_bank = multer.diskStorage({
  // destination
  destination: (req, file, cb) =>  {
   cb(null, './uploads/')
 },
 filename: (req, file, cb) => {
   cb(null, file.originalname);
  }
});

app.post("/upload_bank", upload_bank.array("uploads[]", 12), (req, res) =>  {
  //console.log('files', req.files);
  res.send(req.files);
});