Ich benötigte eine ähnliche Lösung wie die von @alain-cruz, aber in nuxt/vue mit mehreren Downloads. Ich weiß, dass Browser mehrfache Dateidownloads blockieren, und ich habe auch eine API, die eine Gruppe von csv-formatierten Daten zurückgibt. Zuerst wollte ich JSZip verwenden, aber ich brauchte Unterstützung für IE, also hier ist meine Lösung. Wenn jemand mir helfen kann, dies zu verbessern, wäre das großartig, aber bisher funktioniert es für mich.
API gibt zurück:
data : {
body: {
fileOne: ""col1", "col2", "Datensatz1.1", "Datensatz1.2"... usw.",
fileTwo: ""col1", "col2"..."
}
}
page.vue:
export default = {
data() {
return {
fileNames: ['fileOne', 'fileTwo'],
}
},
computed: {
...mapState({
fileOne: (state) => state.exportFile.fileOne,
fileTwo: (state) => state.exportFile.fileTwo,
}),
},
method: {
handleExport() {
//exportFileAction in store/exportFile needs to return promise
this.$store.dispatch('exportFile/exportFileAction', paramsToSend)
.then(async (response) => {
const downloadPrep = this.fileNames.map(async (fileName) => {
// using lodash to get computed data by the file name
const currentData = await _.get(this, `${fileName}`);
const currentFileName = fileName;
return { currentData, currentFileName };
});
const response = await Promise.all(downloadPrep);
return response;
})
.then(async (data) => {
data.forEach(({ currentData, currentFileName }) => {
this.forceFileDownload(currentData, currentFileName);
});
})
.catch(console.error);
},
forceFileDownload(data, fileName) {
const url = window.URL
.createObjectURL(new Blob([data], { type: 'text/csv;charset=utf-8;' }));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', `${fileName}.csv`);
document.body.appendChild(link);
link.click();
},
}