Express req.files总是返回空白[object]

时间:2018-05-05 05:58:09

标签: javascript node.js express file-upload

我尝试将上传的文件保存为其原始名称(使用express-fileupload),当我上传文件时,它将始终保存在[object Object]目录中

节点

var express = require('express')
var app = express();
var bodyParser = require('body-parser')
app.use(fileUpload());

app.post('/upload', function(req, res) {
  if (!req.files)
    return res.status(400).send('No files were uploaded.');
  //define file name
  let audiofile = req.files.audiofile;
  //save the file
  audiofile.mv(__dirname + '/' + audiofile + '.mp3', function(err) {
    if (err)
      return res.status(500).send(err);

    res.send('Audio uploaded!');
  });
});

HTML表单:

<form ref='uploadForm'
      id='uploadForm'
      action='http://localhost:8080/upload'
      method='post'
      encType="multipart/form-data">
      <div class="upload-btn-wrapper">
        <button class="btn whitebg main">Choose audio</button>
        <input class="nb" type="file" name="audiofile" />
      </div>
        <input class="nobtn main white" type='submit' value='Upload!' />
      </form>

以前我使用audiofile.mv(__dirname + '/audiofile.mp3'但很明显它会将每个文件保存为audiofile.mp3,但我不想更改文件名。顺便说一下,我尝试使用req.files.audiofile.name方法,但我得到了name is not defined

1 个答案:

答案 0 :(得分:0)

Sub Test()
    For i = 1 To Rows.Count
        Concatenate_Multiline Range("A" & i), Range("B" & i), Range("C" & i)
    Next i
End Sub

Sub Concatenate_Multiline(cell1 As Range, cell2 As Range, destination As Range)
    Dim lineCell1() As String
    Dim lineCell2() As String
    Dim sResult As String

    lineCell1() = Split(cell1.Formula, vbLf, , vbTextCompare)
    lineCell2() = Split(cell2.Formula, vbLf, , vbTextCompare)

    For i = LBound(lineCell1) To UBound(lineCell1)
        sResult = sResult & lineCell1(i)

        If (i >= LBound(lineCell2)) Then
            If (i <= UBound(lineCell2)) Then
                sResult = sResult & lineCell2(i)

                If (i < UBound(lineCell1)) Then
                    sResult = sResult & vbLf
                End If
            End If
        End If
    Next i

    destination.Formula = sResult
End Sub

let audiofile = req.files.audiofile; 是一个JSON对象或数组,这就是文件保存为audiofile的原因您需要使用[object Object]进行检查或调试console.log内的内容并获取文件名来自audiofile 检查一下

https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON