Update script.js

This commit is contained in:
Voxel 2025-04-13 20:11:20 -04:00 committed by GitHub
parent dafc70d953
commit 1a0f14c4e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4,44 +4,52 @@ async function uploadFile() {
const expiry = document.getElementById("expiry").value;
const timeValue = document.getElementById("expiryTime").value;
// Check if a file is selected
if (!file) {
showAlert("Please choose a file to upload.", "error");
return;
}
try {
// Prepare FormData for file upload
const formData = new FormData();
formData.append("file", file);
formData.append("expiry", expiry);
formData.append("expiryTime", timeValue);
formData.append("file", file); // Attach the file to the formData
formData.append("expiry", expiry); // Add expiry type if selected
if (timeValue) formData.append("expiryTime", timeValue); // Add expiry time if provided
// Replace with your actual Upload.io API URL and API key
const apiUrl = "https://api.upload.io/v1/files"; // Placeholder API URL
const apiUrl = "https://api.upload.io/v1/files"; // Correct API endpoint
const apiKey = "public_G22nhgTDm1B4ZvL1ia7nmepwHaYL"; // Replace with your Upload.io API key
try {
// Perform the file upload request to Upload.io
const response = await fetch(apiUrl, {
method: "POST",
headers: {
"Authorization": `Bearer ${apiKey}`,
"Authorization": `Bearer ${apiKey}`, // Your Upload.io API key for authentication
},
body: formData,
});
// Log the status and response body for debugging
console.log("Response Status:", response.status);
console.log("Response Headers:", response.headers);
const data = await response.json();
if (data && data.url) {
// Handle the response
if (response.ok && data.url) {
const fileLink = document.getElementById("fileLink");
fileLink.href = data.url; // Assuming the response contains the file URL
fileLink.textContent = data.url;
document.getElementById("result").style.display = "block";
showAlert("Success!", "success");
showAlert("File uploaded successfully! See the link below.", "success");
} else {
throw new Error("Failed to upload.");
console.error("Upload failed:", data);
showAlert("Failed to upload file. Please check the console for more details.", "error");
}
} catch (error) {
console.error("Upload failed:", error);
showAlert("Failed to upload.", "error");
console.error("Error:", error);
showAlert("Failed to upload file. Check your internet or try again later.", "error");
}
}