Skip to content
Snippets Groups Projects
Commit ad61be03 authored by Allen Gilliland's avatar Allen Gilliland
Browse files

fixes #1908 by switching the csv download over to using an http POST request.

parent 25f1c08f
Branches
Tags
No related merge requests found
......@@ -132,10 +132,17 @@ export default class QueryVisualization extends Component {
}
}
onDownloadCSV() {
const form = this._downloadCsvForm.getDOMNode();
form.query.value = JSON.stringify(this.props.card.dataset_query);
form.submit();
}
renderDownloadButton() {
const { downloadLink } = this.props;
// NOTE: we expect our component provider set this to something falsey if download not available
// TODO: it's likely the downloading won't work with large query expressions on the Mac App
if (downloadLink) {
const { result } = this.props;
......@@ -148,7 +155,14 @@ export default class QueryVisualization extends Component {
this.refs.downloadModal.toggle()
}}>Download CSV</button>);
} else {
downloadButton = (<a className="Button Button--primary" href={downloadLink} target="_blank" onClick={() => this.refs.downloadModal.toggle()}>Download CSV</a>);
downloadButton = (
<form ref={(c) => this._downloadCsvForm = c} method="POST" action="/api/dataset/csv">
<input type="hidden" name="query" value="" />
<a className="Button Button--primary" onClick={() => {this.onDownloadCSV(); this.refs.downloadModal.toggle();}}>
Download CSV
</a>
</form>
);
}
return (
......@@ -183,9 +197,12 @@ export default class QueryVisualization extends Component {
);
} else {
return (
<a className="mx1" href={downloadLink} title="Download this data" target="_blank">
<Icon name='download' width="16px" height="16px" />
</a>
<form ref={(c) => this._downloadCsvForm = c} method="POST" action="/api/dataset/csv">
<input type="hidden" name="query" value="" />
<a className="mx1" title="Download this data" onClick={() => this.onDownloadCSV()}>
<Icon name='download' width="16px" height="16px" />
</a>
</form>
);
}
}
......
......@@ -45,4 +45,25 @@
{:status 500
:body (:error response)})))
(defendpoint POST "/csv"
"Execute an MQL query and download the result data as a CSV file."
[query]
{query [Required String->Dict]}
(clojure.pprint/pprint query)
(read-check Database (:database query))
(let [{{:keys [columns rows]} :data :keys [status] :as response} (driver/dataset-query query {:executed_by *current-user-id*})
columns (map name columns)] ; turn keywords into strings, otherwise we get colons in our output
(if (= status :completed)
;; successful query, send CSV file
{:status 200
:body (with-out-str
(csv/write-csv *out* (into [columns] rows)))
:headers {"Content-Type" "text/csv"
"Content-Disposition" (str "attachment; filename=\"query_result_" (u/date->iso-8601) ".csv\"")}}
;; failed query, send error message
{:status 500
:body (:error response)})))
(define-routes)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment