简体   繁体   English

使用Fs模块在NodeJs中上载文档:错误:结束后写入

[英]Uploading document in NodeJs using Fs module : Error: write after end

I have been trying to upload a document using the following code. 我一直在尝试使用以下代码上传文档。

I did googled and could not find any solution as yet. 我做了谷歌搜索,还没有找到任何解决方案。

app.post('/documents', function (req, res) {

    var document = {};

    var fstream = null;

    // populate fields
    form.on("field", function (name, value) {
        document[name] = value ;

    });

    form.on("part", function (part) {

        if (!part.filename) {
            return;
        }

        var path = __dirname + '/files/' + part.filename;

         fstream = app.npm.fs.createWriteStream(path);

        document.type = part.headers["content-type"]
        document.name = part.filename;
        document.size = part.byteCount;
        document.path = path;

        fstream.on("close", function () {
            db.sequelize.models.Document.create(document).then(function (newDocument) {
                    res.send(newDocument)
                    res.end();
                },
                function (error) {
                    res.send(error);
                });
        });
        part.pipe(fstream);

    });

    form.on("close", function (data) {

        fstream.end();
        fstream = null;

    });

    form.parse(req);
});

NOTE: I am using fs module. 注意:我正在使用fs模块。 https://nodejs.org/api/fs.html https://nodejs.org/api/fs.html

The first image works ok. 第一张图片正常。 but when I try to upload an another image, it throws an exception: 但是当我尝试上传另一个图像时,它会引发异常:

events.js:72 throw er; events.js:72 throw er; // Unhandled 'error' event ^ //未处理的“错误”事件^

Error: write after end at writeAfterEnd (_stream_writable.js:132:12) at Form.Writable.write (_stream_writable.js:180:5) at write (_stream_readable.js:601:24) at flow (_stream_readable.js:610:7) at IncomingMessage.pipeOnReadable (_stream_readable.js:642:5) at IncomingMessage.emit (events.js:92:17) at emitReadable_ (_stream_readable.js:426:10) at emitReadable (_stream_readable.js:422:5) at readableAddChunk (_stream_readable.js:165:9) at IncomingMessage.Readable.push (_stream_readable.js:127:10) 错误:在Form.Writable.write(_stream_writable.js:180:5)的writeAfterEnd(_stream_writable.js:180:5)在写入(_stream_read.js:601:24)在流(_stream_read.js:610)处结束写入:7)在IncomingMessage.pipeOnReadable(_stream_read.js:642:5)在IncomingMessage.emit(events.js:92:17)在emitReadRead_(_stream_visible.js:426:10)在emitReadRead(_stream_read.js:422:5) )在IncomingMessage.Readable.push(_stream_visible.js:127:10)上​​的可读AddChunk(_stream_visible.js:165:9)上

Found the solution. 找到了解决方案。

var form = new app.npm.multiparty.Form(); var form = new app.npm.multiparty.Form();

This was defined at global level. 这是在全球范围内定义的。 Where as for every new request I should be creating a new instance of the form because stream is off in the previous call so I should create a new instance by getting latest contents. 至于每个新请求,我应该创建表单的新实例,因为在上一次调用中流已关闭,所以我应该通过获取最新内容来创建新实例。

The solution looks likes: 解决方案如下所示:

app.post('/documents', function (req, res) { app.post('/ documents',function(req,res){

var document = {};

var form = new app.npm.multiparty.Form();

var fstream = null;

... ...

And the rest is same. 其余的都一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM