多个 nodejs 模块 onFileUploadStart file.size returns 0

multer nodejs modules onFileUploadStart file.size returns 0

我正在创建一个从 node.js 中的多个客户端接收文件的应用程序。为此,我使用快速多节点模块。

在下面的代码中我处理了文件上传:

app.use(multer({ dest: './uploads/',
    rename: function (fieldname, filename){
        return filename;
    },
    onFileUploadStart: function(file){
        console.log("file size: " + file.size);
        console.log(file.originalname + ' is starting...');
    },
    onFileUploadComplete: function(file){
        console.log(file.fieldname + ' uploaded to ' + file.path);
        done = true;
    },
    onFileUploadData: function(file, data){
        //console.log("data being received " + data);
    }
}));

但是当我在 onFileUploadStart 中输入时,file.size 是 0。

多文件对象是具有以下属性的 JSON 对象,因此我知道大小 属性 存在。

fieldname - Field name specified in the form
originalname - Name of the file on the user's computer
name - Renamed file name
encoding - Encoding type of the file
mimetype - Mime type of the file
path - Location of the uploaded file
extension - Extension of the file
size - Size of the file in bytes
truncated - If the file was truncated due to size limitation
buffer - Raw data (is null unless the inMemory option is true)

为什么返回的尺寸为 0?

编辑: 虽然文件大小 returns 0 文件的其他属性如 file.originalname 正在工作并返回正确的名称。

编辑2: 当我在 onFileUploadStart 函数中打印文件时,我得到以下属性和关联值:

{
    filename: 'userFile',
    originalname: 'name.png',
    name: 'name.png',
    encoding: '7bit',
    mimetype: 'image/png',
    path: 'uploads\name.png',
    extension: 'png',
    size: 0,
    truncated: null,
    buffer: null
}

为什么多节点模块发送的所有内容都正确,但文件大小不正确?

编辑3:

request.body 打印以下内容:

{ submit: 'upload file' }

这很奇怪。 我正在以下代码中打印请求正文:

app.post('/upload/', function(request, response){
    console.log(request.body);
    if (done == true){
        response.end('file uploaded');
    }
});

index.html客户端可以上传文件的文件:

<html>
    <head>

    </head>
    <body>
        <form id= "upload" enctype= "multipart/form-data" action="/upload/" method="post">
            <input type="file" name="userFile" />
            <input type="submit" name="submit" value="upload file" />
        </form>
    </body>
</html>

您只能在上传完成后获得文件总大小,因为文件是流式传输的。

如果您查看 source you can see the initial size is set to 0, then is increased along with the data 事件。