我正在尝试使用mongodump命令从服务器远程下载Mongo转储(即通过浏览器)。
后端是Flask服务器,如下所示:
@api.route('/export', methods=['GET'])
def exportDb():
subprocess.check_output(['mongodump','--archive=db.gz', '--gzip', '--db', 'my_db'])
response = make_response(open('db.gz', 'r').read())
response.headers["Content-Disposition"] = "attachment; filename=db.gz"
return response
前端使用AngularJs,如下所示:
$http({
method: 'GET',
url: '/intro/export'
}).then(function(response) {
var blob = new Blob([response.data], {type: 'application/zip, application/octet-stream'});
var objectUrl = URL.createObjectURL(blob);
window.open(objectUrl);
}
存档是在服务器端正确创建的,但是我无法将其发送给客户端。发送请求后,将打开一个新选项卡,用于下载以GUID命名的文件,而不是“ db.gz”,并且该文件无法使用任何存档客户端打开,因此我在发送文件或保存它。
任何帮助将不胜感激。
所以我这样做是这样的:
@api.route('/exportDB', methods=['GET'])
def exportDB():
subprocess.check_output(['mongodump','--archive=db.gz', '--gzip', '--db', 'my_db'])
response = send_from_directory("path/to/folder", 'db.gz', as_attachment=True)
response.headers["Content-Type"] = "application/javascript"
return response
在客户端,我有:
$http({
method: 'GET',
url: '/intro/exportDB',
responseType: 'blob'
}).then(function(response) {
var data = new Blob([response.data]);
saveAs(data, "db.gz");
}
其中的saveAs是Filesaver.js从这里
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句