Skip to content
Snippets Groups Projects
Commit 57791d03 authored by Xiao Gui's avatar Xiao Gui
Browse files

Merge remote-tracking branch 'origin/pr_avoidUsingb64' into dev_streamDataset

parents 341c1e25 2bf72226
No related branches found
No related tags found
No related merge requests found
......@@ -140,14 +140,8 @@ datasetsRouter.post("/downloadParcellationThemself", (req,res, next) => {
})
}
zip.generateAsync({type:"base64"})
.then(function (content) {
// location.href="data:application/zip;base64,"+content;
res.end(content)
});
res.setHeader('Content-Type', 'application/zip')
zip.generateNodeStream().pipe(res)
});
module.exports = datasetsRouter
\ No newline at end of file
import { Injectable } from "@angular/core";
import {HttpClient} from "@angular/common/http";
import {AtlasViewerConstantsServices} from "src/atlasViewer/atlasViewer.constantService.service";
import {map} from "rxjs/operators";
import {AtlasViewerConstantsServices} from "src/atlasViewer/atlasViewer.constantService.service"
@Injectable({ providedIn: 'root' })
export class ZipFileDownloadService {
constructor(private httpClient: HttpClient, private constantService: AtlasViewerConstantsServices) {}
constructor(private constantService: AtlasViewerConstantsServices) {}
/**
* TODO make naming more generic
*/
downloadZip(publicationsText, fileName, niiFiles) {
const correctedName = fileName.replace(/[|&;$%@"<>()+,/]/g, "")
return this.httpClient.post(this.constantService.backendUrl + 'datasets/downloadParcellationThemself', {
fileName: correctedName,
publicationsText: publicationsText,
niiFiles: niiFiles === 0 ? null : niiFiles
},{responseType: "text"}
).pipe(
map (data => {
this.downloadFile(data, correctedName)
})
)
}
downloadFile(data, fileName) {
const contentType = 'application/zip';
const b64Data = data
const b64toBlob = (b64Data, contentType='', sliceSize=512) => {
const byteCharacters = atob(b64Data);
const byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
const slice = byteCharacters.slice(offset, offset + sliceSize);
const byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
const blob = new Blob(byteArrays, {type: contentType});
return blob;
const body = {
fileName: correctedName,
publicationsText: publicationsText,
niiFiles: niiFiles === 0 ? null : niiFiles
}
const url = `${this.constantService.backendUrl}datasets/downloadParcellationThemself`
return fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body),
})
.then(res => res.blob())
.then(data => {
this.simpleDownload(data, correctedName)
})
}
const blob = b64toBlob(b64Data, contentType);
simpleDownload(data, filename) {
const blob = new Blob([data], { type: 'application/zip'})
const url= window.URL.createObjectURL(blob);
const anchor = document.createElement("a");
anchor.download = fileName + '.zip';
anchor.download = filename + '.zip';
anchor.href = url;
anchor.click();
}
}
\ No newline at end of file
......@@ -85,7 +85,9 @@ export class ReferenceToastComponent implements OnInit{
this.zipFileDownloadService.downloadZip(
publicationsText,
fileName,
this.parcellationNifti? this.parcellationNifti : 0).subscribe(data => {
this.parcellationNifti? this.parcellationNifti : 0
)
.then(() => {
this.downloadingProcess = false
})
publicationsText = ''
......
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