At one point in my ember.js
webapp the user is given the option to download a zip file. On clicking the button, an action is triggered that sends a request to the backend server, which generates the zip and returns it. Ideally the zip should then be downloaded automatically.
In my backend endpoint I return with
return Response
.ok(FileUtils.readFileToByteArray(new File(tmpZipFilename))) // tmpZipFilename is a String
.type("application/zip")
.header("Content-Disposition", "attachment; filename=\"" + finalZipFilename + "\"")
.build();
In the frontend I have (adapted from here)
submit() {
var formData = new FormData(this);
let token = this.get('session.data.authenticated.token');
jquery.ajax({
url: `myUrl`,
data: formData,
processData: false,
contentType: false,
beforeSend: function(xhr) {xhr.setRequestHeader('Authorization', `Bearer ${token}`)},
type: 'POST',
success: function(data) {
var blob = new Blob([data], {type: 'application/zip'});
let a = document.createElement("a");
a.style = "display: none";
document.body.appendChild(a);
let url = window.URL.createObjectURL(blob);
a.href = url;
a.download = 'myFile.zip';
a.click();
window.URL.revokeObjectURL(url);
},
failure: function() {
// other stuff
}
})
}
The response headers are the following:
HTTP/1.1 200 OK
X-Powered-By: Undertow/1
Cache-Control: no-store
Date: Tue, 19 Feb 2019 16:34:35 GMT
Server: WildFly/10
Content-Type: application/zip
Content-Disposition: attachment; filename="filename.zip"
Connection: close
Transfer-Encoding: chunked
I have confirmed that tmpZipFilename
in the backend section does correctly point to a proper zip file. And when the user clicks the download button a file called myFile.zip
is indeed downloaded. However, the file downloaded is not unzippable and is a different size than the correct file pointed to by tmpZipFilename
. What am I doing wrong?
Aucun commentaire:
Enregistrer un commentaire