下载使用 mongodump 创建的存档
Downloading an archive created with mongodump
我正在尝试使用 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");
}
其中另存为来自 Filesaver.js 来自 here
我正在尝试使用 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");
}
其中另存为来自 Filesaver.js 来自 here