download frontend fix

download frontend fix
This commit is contained in:
Benjamin Loh 2023-10-23 18:42:19 +08:00
parent 4a6b540704
commit d8f3758d0b
2 changed files with 25 additions and 22 deletions

View File

@ -12,32 +12,35 @@ function DownloadFile() {
const handleDownloadFile = () => { const handleDownloadFile = () => {
if (passcode) { if (passcode) {
// Send an API request to your backend with the passcode axios.get(`http://127.0.0.1:8000/api/files/${passcode}/`)
axios.get(`http://127.0.0.1:8000/api/files/${passcode}`, { responseType: 'blob' }) .then(response => {
.then((response) => { // print json data from response
// Create a blob from the response data console.log(response.data);
const blob = new Blob([response.data], { type: response.headers['content-type'] }); const fileData = response.data.file;
if (fileData) {
// Convert the data to a Blob
const blob = new Blob([fileData], { type: 'application/octet-stream' });
// Create a URL for the blob // Create a temporary URL for the Blob
const url = window.URL.createObjectURL(blob); const url = window.URL.createObjectURL(blob);
// Create a temporary anchor element for downloading the file // Create a dynamically generated anchor element
const a = document.createElement('a'); const a = document.createElement('a');
a.href = url; a.href = url;
a.download = response.data.filename; // TODO: Set the file name below
a.click(); a.download = response.data.name;
// Revoke the URL to release resources // Trigger a click event on the anchor element to simulate a download
window.URL.revokeObjectURL(url); a.click();
// Clean up the temporary URL
window.URL.revokeObjectURL(url);
}
}) })
.catch((error) => { .catch(error => {
// Handle errors, e.g., show an error message // print error if any
console.error('File download failed', error); console.log(error);
}); });
} else {
// Handle the case when passcode is null or empty
console.error('Passcode is required.');
// You can show an error message or take other actions as needed.
} }
}; };

View File

@ -14,7 +14,7 @@ function ShareFile() {
if (file) { if (file) {
const formData = new FormData(); const formData = new FormData();
formData.append('file', file); formData.append('file', file);
formData.append('ttl', "60"); //formData.append('ttl', "60");
// Send POST request to the backend API using Axios // Send POST request to the backend API using Axios
axios.post('http://127.0.0.1:8000/api/files/', formData) axios.post('http://127.0.0.1:8000/api/files/', formData)