Use ContentDisposition instead of ContentType to control download (#2722)

* Use ContentDisposition instead of ContentType to control download

Fixes #1197. Previously we were using a funky ContentType to attempt
to force a file download rather than display in browser, but this
conflicted with attempts to save UTF-8 which was outside the Basic
Multilingual Plane (BMP).

By switching to ContentDisposition: attachment, which has been
the preferred method for a number of years, we can avoid this conflict.

As part of this, switch to using the "preview" param consistently
to control preview vs download rather than the content type.

* Switch content type to text/plain

Now that we don't need to use ContentType to control download
behavior, we can use something more reasonable.
This commit is contained in:
Tom Morris 2020-06-16 09:46:07 -04:00 committed by GitHub
parent 66c409aec6
commit 5f368bc56d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 20 deletions

View File

@ -101,6 +101,13 @@ public class ExportRowsCommand extends Command {
}
response.setHeader("Content-Type", contentType);
String preview = params.getProperty("preview");
if (!"true".equals(preview)) {
String path = request.getPathInfo();
String filename = path.substring(path.lastIndexOf('/') + 1);
response.setHeader("Content-Disposition", "attachment; filename=" + filename);
}
if (exporter instanceof WriterExporter) {
String encoding = params.getProperty("encoding");

View File

@ -57,7 +57,7 @@ public class TemplatingExporter implements WriterExporter {
@Override
public String getContentType() {
return "application/x-unknown";
return "text/plain";
}
protected static class TemplateConfig {

View File

@ -314,12 +314,10 @@ CustomTabularExporterDialog.prototype._postExport = function(preview) {
.attr("value", encoding)
.appendTo(form);
}
if (!preview) {
$('<input />')
.attr("name", "contentType")
.attr("value", "application/x-unknown") // force download
.appendTo(form);
}
$('<input />')
.attr("name", "preview")
.attr("value", preview)
.appendTo(form);
document.body.appendChild(form);

View File

@ -357,14 +357,11 @@ function SqlExporterDialog(options) {
.attr("value", encoding)
.appendTo(form);
}
if (!preview) {
$('<input />')
.attr("name", "contentType")
.attr("value", "application/x-unknown") // force download
.appendTo(form);
}
$('<input />')
.attr("name", "preview")
.attr("value", preview)
.appendTo(form);
// alert("form::" + form);
document.body.appendChild(form);
window.open(" ", "refine-export");

View File

@ -111,10 +111,6 @@ ExporterManager.stripNonFileChars = function(name) {
ExporterManager.handlers.exportRows = function(format, ext) {
var form = ExporterManager.prepareExportRowsForm(format, true, ext);
$('<input />')
.attr("name", "contentType")
.attr("value", "application/x-unknown") // force download
.appendTo(form);
document.body.appendChild(form);