我可以使用以下代码通过浏览器成功下载csv文件。
main.py摘录:
@app.route('/downloadSimulationScore')
def download_simulation_score():
userId = request.args.get('userid')
download_path = 'data/simulation/'+userId+'/simCombinedOutputForDownload.csv'
return send_file(download_path,
attachment_filename='simulation_score.csv')
simulation.js摘录:
function downloadSimulation(){
$.ajax({
type: "GET",
url: server + "/downloadSimulationScore?userid=" +USERID,
dataType: "text",
cache: false,
success: function(data) {
writeLog("INFO", "Simulation: Downloaded simulation score csv");
// create file blob
var binaryData = [];
binaryData.push(data);
var blob = new Blob(binaryData, {type:"text/csv"});
// download file
if(navigator.msSaveBlob){
// if IE
navigator.msSaveBlob(blob, 'simulation_score.csv');
}
else {
var a = document.createElement('a');
var fileurl = window.URL.createObjectURL(blob);
a.href = fileurl;
a.download = 'simulation_score.csv';
a.click();
}
// show alert
$("#mainAlert").text("CSV file downloaded.")
$("#mainAlert").addClass('alert--show');
setTimeout(function(){
$("#mainAlert").removeClass('alert--show');
}, 5000);
// change select back to default
$("#downloadCSV").val("none")
},
error: function(err){
console.log(err);
// show alert
$("#mainAlert").text("No csv data for this day")
$("#mainAlert").removeClass('alert--success');
$("#mainAlert").addClass('alert--show');
setTimeout(function(){
$("#mainAlert").removeClass('alert--show');
$("#mainAlert").addClass('alert--success');
}, 5000);
// change select back to default
$("#downloadCSV").val("none");
}
});
}
我正在尝试更改此设置,以便可以下载pdf而不是csv。为此,我将上述功能更改为:
修订后的Simulation.js摘录:
// download simulation score
function downloadSimulation(){
$.ajax({
type: "GET",
url: server + "/downloadSimulationScore?userid=" +USERID,
dataType: "text",
cache: false,
contentType : 'application/pdf',
success: function(data) {
writeLog("INFO", "Simulation: Downloaded simulation report pdf");
// create file blob
var binaryData = [];
binaryData.push(data);
var blob = new Blob([data], {type:"application/pdf"});
// download file
if(navigator.msSaveBlob){
// if IE
navigator.msSaveBlob(blob, 'metis_simulation_report.pdf');
}
else {
var a = document.createElement('a');
var fileurl = window.URL.createObjectURL(blob);
a.href = fileurl;
a.download = 'metis_simulation_report.pdf';
document.body.appendChild(a);
a.click();
}
// show alert
$("#mainAlert").text("PDF file downloaded.")
$("#mainAlert").addClass('alert--show');
setTimeout(function(){
$("#mainAlert").removeClass('alert--show');
}, 5000);
// change select back to default
$("#downloadCSV").val("none")
},
error: function(err){
console.log(err);
// show alert
$("#mainAlert").text("No csv data for this day")
$("#mainAlert").removeClass('alert--success');
$("#mainAlert").addClass('alert--show');
setTimeout(function(){
$("#mainAlert").removeClass('alert--show');
$("#mainAlert").addClass('alert--success');
}, 5000);
// change select back to default
$("#downloadCSV").val("none");
}
});
}
这将下载chrome中的空白pdf(无错误消息)。通过ie下载的pdf给出了以下错误消息(在Adobe Acrobat中):
发生了文件I / O错误。
任何帮助将不胜感激。