0

我正在编写一个接收表单的应用程序,但我遇到了一个小问题。当没有文件上传时,我的应用程序冻结。我不知道为什么。我使用 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;
                    }
            }
        });

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

任何帮助表示赞赏。谢谢!

4

0 回答 0