后台
import base64
@http.route('/api/studio/home/export2', type='json', auth='user')
def export2(self, models):
"""
导出模型列表,生成代码,压缩为zip包
@param models: 需要导出的模型技术名称列表,中间用`,`隔开
@return zip代码包
"""
if not isinstance(models, list):
raise ValidationError(_('Parameter models must be a non-empty list.'))
# 将需要导出的模型数据导出
models_data = {model[0]: request.env[model[0]].with_context(lang='es_ES').search([(model[1], 'in', models)])
for model in MODELS_TO_EXPORT
if isinstance(model, (list, tuple)) and len(model) == 2 and model[0] != 'studio.app.version'}
content = StudioExport(models_data).generate_zip()
content_binary = content.decode('utf-8')
content_base64 = base64.encodestring(content_binary)
header = 'data:application/zip;base64,'
return header + str(content_base64)[2:-1]
前端
this.$rpc({
route: '/home/export2',
params: {
'models': ['gl.voucher']
}
}).then((res) => {
const content = res;
// let buffer = Buffer.from(content, 'utf-8');
// let base64Str = buffer.toString('base64');
// console.log('base64编码后的字符串: '+base64Str); // 输出:base64编码后的字符
const blob = new Blob([content],{type:'application/zip'});
const fileName = 'aaa' ;
if ('download' in document.createElement('a')) { // 非IE下载
const elink = document.createElement('a');
elink.download = fileName;
elink.style.display = 'none';
elink.href = window.URL.createObjectURL(blob);
console.log('elink.href', elink.href);
document.body.appendChild(elink);
elink.click();
window.URL.revokeObjectURL(elink.href); // 释放URL 对象
document.body.removeChild(elink);
} else { // IE10+下载
navigator.msSaveBlob(blob, fileName);
}
});
this.$rpc({
route: ‘/home/export2’,
params: {
‘models’: [‘gl.voucher’]
}
}).then((res) => {
let blob = new Blob([res],{type:’application/zip’});
const elink = document.createElement(‘a’);
elink.download = ‘aaa’;
elink.style.display = ‘none’;
elink.href = window.URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
window.URL.revokeObjectURL(elink.href); // 释放URL 对象
document.body.removeChild(elink);
});