Nodejs POST无法接收文件

时间:2018-07-04 02:16:01

标签: node.js express google-cloud-functions multer formidable

我想创建一个API以接收来自网络的上传文件。然后,我将对文件进行检查(以查看.png是否是真正的.png而不是.exe),然后将文件发送到另一个API。

但是现在我被困在接收文件部分。

我一直在google和stackoverflow中搜索,但是这两种方法都不起作用。

我在哪里托管我的API: Firebase函数

示例代码:

html代码:

<html>
  <body>
    <form ref='uploadForm' 
      id='uploadForm' 
      action='https://<project>.cloudfunctions.net/api/upload' 
      method='post' 
      encType="multipart/form-data">
        <input type="file" name="sampleFile" />
        <input type='submit' value='Upload!' />
    </form>     
  </body>
</html>

NodeJS代码:

formidable

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

app.post('/upload', function (req, res) {
    console.log('initializing upload')
    var IncomingForm = require('formidable').IncomingForm

    var form = new IncomingForm()
    form.uploadDir = 'uploads'
    form.parse(req, function (err, fields, files) {
        if (err) {
            console.log('some error', err)
            res.status(400).send(err)
        } else if (!files.file) {
            console.log('no file received')
            res.status(400).send('no file received')
        } else {
            var file = files.file
            console.log('saved file to', file.path)
            console.log('original name', file.name)
            console.log('type', file.type)
            console.log('size', file.size)
            res.status(400).send(file.path, file.name, file.type, file.size)
        }
    })
});

exports.api = functions.https.onRequest(app)

connect-busboy

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

var fs = require('fs');
var busboy = require('connect-busboy');
//...
app.use(busboy()); 
//...
app.post('/upload', function(req, res) {
    var fstream;
    req.pipe(req.busboy);
    req.busboy.on('file', function (fieldname, file, filename) {
        console.log("Uploading: " + filename); 
        res.status('200').send('uploaded')
        // fstream = fs.createWriteStream(__dirname + '/files/' + filename);
        // file.pipe(fstream);
        // fstream.on('close', function () {
        //     res.redirect('back');
        // });
    });
});

exports.api = functions.https.onRequest(app)

express-fileupload

const functions = require('firebase-functions');
const express = require('express');
const fileUpload = require('express-fileupload');
const app = express();

// default options
app.use(fileUpload());

app.post('/upload', function(req, res) {
    console.log('upload initializing')
  if (!req.files)
    return res.status(400).send('No files were uploaded.');

  // The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file
  let sampleFile = req.files.sampleFile;

  if(req.files){
    console.log('File Uploaded')
    res.status('200').send('File uploaded!');
  }

});

exports.api = functions.https.onRequest(app)

multer

const functions = require('firebase-functions');
const express = require('express'); // call express
const app = express(); // define our app using express
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const router = express.Router();

const multer = require('multer');
var storage = multer.memoryStorage()
var upload = multer({ storage: storage })

const readChunk = require('read-chunk');
const fileType = require('file-type');


var returnSuccess = {
    resultCode: 1,
    resultDesc: '',
    resultData: '',
    dataCount: 0,
    resultAction: '',
    timeStamp: Date.now()
}

var returnFailed = {
    resultCode: -1,
    resultDesc: '',
    resultAction: '',
    timeStamp: Date.now()
}

router.post('/fileChecker', upload.single('doc'), function (req, res, next) {
    console.log('Initilize FileChecker')
    var files = []
    var checkResults = []
    var file = req.file
    files = req.files
    console.log(file)
    console.log(files)
    if(file){
        console.log('got the FILE!')
    }

    res.status(200).send('Processed Ntg')
});


app.use(router)

exports.api = functions.https.onRequest(app)

*附加说明:

1)对于multer,firebase托管似乎与dest选项不兼容。如果设置dest选项,将会出现部署错误。

2)所有代码在部署到Firebase托管后都经过测试,我没有在本地进行测试。


我不知道发生了什么问题。.请帮助..

2 个答案:

答案 0 :(得分:0)

对不起,不是一个完整的答案,但太冗长了,无法评论。只是为了测试multer是否正常工作,请尝试使用通用的中间件并将其放在您的中间件链中(我在cors处理之后但在人体解析之前拥有我的):

onFileUploadStart(file / *,req,res * /){         console.log('multer.onFileUploadStart!',文件); 绝对可以捕获任何发送文件的东西。

编辑:我正在使用旧版本和不推荐使用的东西,看来...抱歉。


哪个又一次查看您的代码?您的CORS处理在哪里?您需要它来满足跨域,跨协议甚至跨端口的浏览器请求。

在此处https://enable-cors.org/server_expressjs.html或尝试cors package

How to allow CORS?

答案 1 :(得分:0)

Multer:内存存储引擎将文件作为<li draggable="true">Tab</li> ... $('.tabpane__tabs li').on('dragover', function(e) { alert('test'); // Does not trigger e.preventDefault(); }); $('.tabpane__tabs li').on('dragstart', function(e) { e.originalEvent.dataTransfer.setData('text/html', $('.tabpane__panes > li').eq($(this).index()).html()); }); $('.tabpane__tabs li').on('drop', function(e) { alert('test'); // Does not trigger }); 对象存储在内存中。它没有任何选择。 使用内存存储时,文件信息将包含一个名为Buffer的字段,其中包含整个文件。 See docs

因为您正在执行内存上载。看看您是否真的要上传到内存或磁盘?