返回列表

node createReadStream 不释放 及 express 仅匹配根目录

默认分类 2015/11/06 07:58

1.node createReadStream 不释放 文件 导致文件 无法删除

注:在createReadStream之后尽量不要把结果赋给另一个异步程序,否则容易出现文件 无法删除

exports.saveIdCardPhotoBase64 = function (req, res, next) {
// parse a file upload 
var form = new formidable.IncomingForm();
form.uploadDir = config.upload_tmp;
form.keepExtensions = true;
form.multiples = true;

form.parse(req, function (err, fields, files) {
    var data = fields['idCardPicBase64'];
    // fs.createReadStream('public/id_card.png')
    if (!data) {
        return res.end('no file upload.');
    }

    var imageBuffer = decodeBase64Image(data);
    var tmp = [
        config.upload_tmp + '/',
        formatDate(new Date(), 'yyyyMMddHHmmss') + String(Math.random()).replace('0.', ''),
        '.' + imageBuffer.type.split('/')[1]
    ].join('');

    // var ff = fs.createReadStream(tmp);

    function callback() {
        var filelist = {};
        if (fs.existsSync(tmp)) {
            // filelist['idCardPic'] = ff; // 这里容易出现文件无法删除!!
            filelist['idCardPic'] = tmp;
            uploadToRemoteImageServer(filelist, function (result) {
                res.send(result);
            });
        }
    }

    fs.writeFile(tmp, imageBuffer.data, function (err) {
        !err ? callback(): res.end('tmp file write fail.');
    });

});

};

2.express 仅匹配根目录

app.get('/?', require('../controller/upload').upload);