Multer无法在Firebase托管的FirebaseApp中工作

时间:2018-08-25 05:26:06

标签: javascript node.js firebase multer

Multer在简单的nodejs应用程序中工作,但是当我尝试在firebaseapp中使用multer时,相同的代码无法正常工作。我只添加了一行。 const functions = require('firebase-functions'); 因此,它表明req.file是未定义的。因此,任何人都告诉我如何解决此问题,或者向我提出可以替代multer并在firebaseapp中正常工作的其他任何软件包。 谢谢。

const functions = require('firebase-functions');

const express = require('express');
const multer = require('multer');
const ejs = require('ejs');
const path = require('path');

var upload = multer({ dest: __dirname })
// Init app
const app = express();

// EJS
app.set('view engine', 'ejs');

// Public Folder
app.use(express.static('./public'));

app.get('/', (req, res) => res.render('index'));

//upload file testing
app.post('/upload', upload.single('myImage'), function (req, res, next) {
  // req.file is the `avatar` file
  // req.body will hold the text fields, if there were any
  if(req.file){console.log(req.file.originalname)}
  else {console.log("file not found")}

  res.send(req.file.originalname)
})

exports.app = functions.https.onRequest(app);
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css">
  
  <title>Node File Uploads</title>
</head>
<body>
  <div class="container">
    <h1>File Upload</h1>
    <%= typeof msg != 'undefined' ? msg : '' %>
    <form action="/upload" method="POST" enctype="multipart/form-data">
      <div class="file-field input-field">
        <div class="btn grey">
          <span>File</span>
          <input name="myImage" type="file">
        </div>
        <div class="file-path-wrapper">
          <input class="file-path validate" type="text">
        </div>
      </div>
      <button type="submit" class="btn">Submit</button>
    </form>
    <br>
    <img src="<%= typeof file != 'undefined' ? file : '' %>" class="responsive-img">
  </div>

  <script
  src="https://code.jquery.com/jquery-3.2.1.min.js"
  integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
  crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>  
</body>
</html>

0 个答案:

没有答案