节点JS客户端代码下载文件

时间:2017-06-22 10:36:24

标签: javascript node.js

我有以下节点js代码转储mysql数据库并将zip文件保存在服务器上:

            /* DUMP - database */
            var mysqldump = child_process.exec(['mysqldump -u root --password='+db_pass+' '+db_name+' | zip --password '+db_zip+' > '+process.env.PWD+'/'+rows[0].name+'-'+date+'.zip']);

            /* DUMP - zip created */
            mysqldump.on('exit', function(){
                /* DOWNLOAD - backup zip */
                res.writeHead(200, {
                    'Content-Disposition': 'attachment;filename='+rows[0].name+'-'+date+'.zip',
                    'Content-Type': 'application/x-gzip',
                    'Cache-Control': 'no-cache'
                });

                /* PIPE - backup zip */
                var zip = fs.createReadStream(process.env.PWD+'/'+rows[0].name+'-'+date+'.zip', 'utf-8').pipe(res);

                /* DELETE - backup zip */
                zip.on('finish', function() {
                    fs.unlink(process.env.PWD+'/'+rows[0].name+'-'+date+'.zip', function() {});
                    res.end();
                });
            });

文件在服务器上成功创建,但我需要客户端代码才能下载此文件..如果我手动链接在chrome中下载文件,它下载文件...我尝试使用:

location ='http://'+window.location.host+'/backup';

它下载文件但是在打开zip文件时它表示错误的标题和文件已损坏...所以问题是:

如何在客户端读取javascript文件并使用zip扩展名保存,以便下载文件正确?读取管道输出二进制文件并在下载完成时将其保存为客户端的zip文件。

更新的完整服务器代码:

/**
 * DEFINE
 * @backup create
 */
panel.get('/backup', function(req, res) {

        /* DEFINE - variables */
        var date = new Date().getFullYear() + "" + ("0" + (new Date().getMonth() + 1)).slice(-2) + "" + ("0" + (new Date().getDate())).slice(-2);

        /* READ - settings */
        pool.query("SELECT name FROM settings",function(err, rows, fields){
            /* CHECK - error */
            if (err) {
                res.json({"code" : 100, "status" : err});
                return;
            };
            /* FOUND - settings */
            if (rows.length) {
                /* DUMP - database */
                var mysqldump = child_process.exec(['mysqldump -u root --password='+db_pass+' '+db_name+' | zip --password '+db_zip+' > '+process.env.PWD+'/'+rows[0].name+'-'+date+'.zip']);

                /* DUMP - zip created */
                mysqldump.on('exit', function(){
                    /* DOWNLOAD - backup zip */
                    res.writeHead(200, {
                        'Content-Disposition': 'attachment;filename='+rows[0].name+'-'+date+'.zip',
                        'Content-Type': 'application/x-gzip',
                        'Cache-Control': 'no-cache'
                    });

                    /* PIPE - backup zip */
                    var zip = fs.createReadStream(process.env.PWD+'/'+rows[0].name+'-'+date+'.zip', 'utf-8').pipe(res);

                    /* DELETE - backup zip */
                    zip.on('finish', function() {
                        fs.unlink(process.env.PWD+'/'+rows[0].name+'-'+date+'.zip', function() {});
                        res.end();
                    });
                });
            } else {
                /* RETURN - settings error */
                res.json({"status": "false", "message": "Backup NOT Available!"});
            }
        });
});

0 个答案:

没有答案