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 = () => {
if (passcode) {
// Send an API request to your backend with the passcode
axios.get(`http://127.0.0.1:8000/api/files/${passcode}`, { responseType: 'blob' })
.then((response) => {
// Create a blob from the response data
const blob = new Blob([response.data], { type: response.headers['content-type'] });
axios.get(`http://127.0.0.1:8000/api/files/${passcode}/`)
.then(response => {
// print json data from response
console.log(response.data);
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);
// Create a temporary anchor element for downloading the file
// Create a dynamically generated anchor element
const a = document.createElement('a');
a.href = url;
a.download = response.data.filename;
// TODO: Set the file name below
a.download = response.data.name;
// Trigger a click event on the anchor element to simulate a download
a.click();
// Revoke the URL to release resources
// Clean up the temporary URL
window.URL.revokeObjectURL(url);
}
})
.catch((error) => {
// Handle errors, e.g., show an error message
console.error('File download failed', error);
.catch(error => {
// print error if any
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) {
const formData = new FormData();
formData.append('file', file);
formData.append('ttl', "60");
//formData.append('ttl', "60");
// Send POST request to the backend API using Axios
axios.post('http://127.0.0.1:8000/api/files/', formData)