没有文件上传时我的应用程序使用busboy冻结

时间:2015-11-14 18:48:31

标签: javascript node.js file multipartform-data busboy

我正在写一个接收表单的应用程序,我有一点问题。没有文件上传时我的应用程序冻结。我不知道为什么。我使用connect-busboy来接收文件。

那是我的代码:

req.busboy.on('file', function(fieldname, file, filename) {
            switch(fieldname){
                case 'mainPhoto':
                    if(filename != '' && filename != null && filename != "null") {
                        console.log("w mainPhoto w if'ie")
                        var n = filename.lastIndexOf(".");
                        var end = filename.substring(n+1, filename.length);
                        mainPhoto = "mh_" + id + "." + end;
                        fstream = fs.createWriteStream(__dirname + '/static/photos/' + mainPhoto);
                        file.pipe(fstream);
                        fstream.on('close', function() {
                            console.log("End of saving hospital's main photo.");
                        });
                        break;
                    }
                    else {
                        mainPhoto = null
                        break;
                    }

                case 'photos':
                    if(filename != '' && filename != null && filename != "null") {
                        var n = filename.lastIndexOf(".");
                        var end = filename.substring(n+1, filename.length);
                        var photohoto;
                        photo = "h_" + id + "_" + photosCount + "." + end;
                        photos.push(photo);
                        fstream = fs.createWriteStream(__dirname + '/static/photos/' + photo);
                        file.pipe(fstream);
                        photosCount++;
                        fstream.on('close', function() {
                            console.log("End of saving hospital's main photo.");
                        });
                        break;
                    }
                    else {
                        break;
                    }


                case 'priceList':
                    if(filename != '' && filename != null && filename != "null") {
                        var n = filename.lastIndexOf(".");
                        var end = filename.substring(n+1, filename.length);
                        priceList = "pricelist_" + id + "." + end;
                        fstream = fs.createWriteStream(__dirname + '/static/price_lists/' + priceList);
                        file.pipe(fstream);
                        fstream.on('close', function() {
                            console.log("End of saving hospital's price list.");
                        });
                        break;
                    }
                    else {
                        priceList = null;
                        break;
                    }
            }
        });

我必须添加所有文件以避免它,但这不是我想要做的事情。

感谢任何帮助。谢谢!

0 个答案:

没有答案