Commit 39d8581c authored by Franziska Koehn's avatar Franziska Koehn
Browse files

start download via jquery download

parent c3af6d0e
......@@ -78,11 +78,10 @@ def set_root_type():
@app.route("/results")
def results(res):
from xsa.datatypereader import get_field_label_by_key
from xsa.queries import prepare_rest
from xsa.datatypereader import get_rest
from xsa.datatypereader import get_comparison_extra_source
from urllib.parse import quote
import json
if state["roottype"] and res is not None:
roottypefields=[]
for h in res.headers():
......@@ -91,8 +90,6 @@ def results(res):
rest_downl = ""
try:
rest_downl = get_rest(state["roottype"])
for r in res:
r['rest'] = quote(prepare_rest(r, rest_downl, state["host"]))
except:
pass
......@@ -106,10 +103,28 @@ def results(res):
else:
flash("No root-type was chosen and/or no search-results are available.")
@app.route("/download", methods=["GET"])
@app.route("/downl_csv", methods=["POST", "GET"])
def download_csv():
from datetime import datetime
response = Response(state['results_export'].dumps_csv(","), mimetype='text/csv')
filename= "search_export_%s.csv" % str(datetime.now())
response.headers["Content-Disposition"] = "attachment; filename="+filename #TODO rename file
return response
@app.route("/download", methods=["POST"])
def download():
from xsa.queries import download_file_iter
url = request.args.get("url")
from xsa.queries import prepare_rest
from xsa.datatypereader import get_rest
import json
from urllib.parse import unquote
res = json.loads(unquote(request.form.get("result")))
url = prepare_rest(res, get_rest(state["roottype"]), state["host"])
creds = state["username"], state["password"]
response = Response(download_file_iter(url, creds), mimetype='application/zip')
response.headers["Content-Disposition"] = "attachment; filename=test.zip"#TODO rename file
......@@ -222,14 +237,6 @@ def downl_query():
return response
@app.route("/downl_csv", methods=["POST", "GET"])
def download_csv():
from datetime import datetime
response = Response(state['results_export'].dumps_csv(","), mimetype='text/csv')
filename= "search_export_%s.csv" % str(datetime.now())
response.headers["Content-Disposition"] = "attachment; filename="+filename #TODO rename file
return response
def start_web_gui(args):
if args.host:
state["host"] = args.host
......
......@@ -55,7 +55,6 @@
var newDoc = document.open("text/html", "replace");
newDoc.write(data);
newDoc.close();
window.history.pushState("", $("title").text(), "/get_adddata");
},
data:{}, method:"POST" });
};
......@@ -98,6 +97,12 @@
$(this).parents('p').remove();
update_chart();
};
var download_result = function(result){
console.log(result);
$.download("/download", {"result":JSON.stringify(result)}, "POST");
};
</script>
</head>
......@@ -156,9 +161,9 @@
<tbody>
{% for r in results %}
<tr>
{% if r["rest"] %}
{% if rest_downl%}
<td style="text-align: center; vertical-align: middle;">
<a href="/download?url={{r['rest']}}"><button type="button" class="btn btn-lg btn-primary" >Download</button></a>
<button type="button" onclick="download_result({{r}})" class="btn btn-lg btn-primary" >Download</button>
</td>
{% endif %}
......@@ -231,3 +236,24 @@
<script src="static/ie10-viewport-bug-workaround.js"></script>
<svg style="visibility: hidden; position: absolute; top: -100%; left: -100%;" preserveAspectRatio="none" viewBox="0 0 200 200" height="200" width="200"><defs></defs><text style="font-weight:bold;font-size:10pt;font-family:Arial, Helvetica, Open Sans, sans-serif;dominant-baseline:middle" y="10" x="0">200x200</text></svg></body></html>
<script>
// http://www.filamentgroup.com/lab/jquery-plugin-for-requesting-ajax-like-file-downloads.html
jQuery.download = function(url, data, method){
//url and data options required
if( url && data ){
//data can be string of parameters or array/object
data = typeof data == 'string' ? data : jQuery.param(data);
//split params into form inputs
var inputs = '';
jQuery.each(data.split('&'), function(){
var pair = this.split('=');
inputs+='<input type="hidden" name="'+ pair[0] +'" value="'+ pair[1] +'" />';
});
//send request
jQuery('<form action="'+ url +'" method="'+ (method||'post') +'">'+inputs+'</form>')
.appendTo('body').submit().remove();
};
};
</script>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment