feat: File download done.

This commit is contained in:
chiayin
2024-01-15 17:18:45 +08:00
parent c64d243de2
commit 9753ed535d
3 changed files with 150 additions and 9 deletions

View File

@@ -151,9 +151,9 @@ export default defineStore('filesStore', {
uploadloader(); // 進度條
try {
const response = await axios.post(api, data);
this.uploadLogId = response.data.id;
this.uploadLogId = response.data.id;
Swal.close(); // 關閉進度條
await this.rename(); // 改檔名
this.rename(); // 改檔名
await uploadSuccess();
this.$router.push({name: 'Files'});
} catch(error) {
@@ -164,6 +164,8 @@ export default defineStore('filesStore', {
Swal.close(); // 關閉進度條
apiError(error, 'Failed to upload the log files.');
}
} finally {
// await this.rename();
}
},
/**
@@ -203,6 +205,72 @@ export default defineStore('filesStore', {
} catch(error) {
apiError(error, 'Failed to rename.');
}
}
},
/**
* Delete file
* @param { string } type log | filter | log-check | filter-check
* @param { number } id
*/
async deleteFile(type, id) {
let api;
switch (type) {
case 'log':
api = `/api/logs/${id}`;
break;
case 'filter':
api = `/api/filters/${id}`;
break;
case 'log-check':
api = `/api/log-checks/${id}`;
break;
case 'filter-check':
api = `/api/filter-checks/${id}`;
break;
}
try {
const response = await axios.delete(api);
} catch(error) {
apiError(error, 'Failed to delete.');
}
},
/**
* Download file as CSV
* @param { string } type log | filter | log-check | filter-check
* @param { number } id
* @param { string } fileName
*/
async downloadFileCSV(type, id, fileName) {
let api;
switch (type) {
case 'log':
api = `/api/logs/${id}/csv`;
break;
case 'filter':
api = `/api/filters/${id}/csv`;
break;
case 'log-check':
api = `/api/log-checks/${id}/csv`;
break;
case 'filter-check':
api = `/api/filter-checks/${id}/csv`;
break;
}
try {
const response = await axios.get(api);
const csvData = response.data;
const blob = new Blob([csvData], { type: 'text/csv' });
const url = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = `${fileName}.csv`;
link.click();
window.URL.revokeObjectURL(url);
} catch(error) {
apiError(error, 'Failed to delete.');
}
},
},
})