mirror of
https://github.com/milk-net/milk-net.github.io.git
synced 2025-04-19 17:43:42 -05:00
Update script.js
This commit is contained in:
parent
dafc70d953
commit
1a0f14c4e1
1 changed files with 23 additions and 15 deletions
|
@ -4,44 +4,52 @@ async function uploadFile() {
|
||||||
const expiry = document.getElementById("expiry").value;
|
const expiry = document.getElementById("expiry").value;
|
||||||
const timeValue = document.getElementById("expiryTime").value;
|
const timeValue = document.getElementById("expiryTime").value;
|
||||||
|
|
||||||
|
// Check if a file is selected
|
||||||
if (!file) {
|
if (!file) {
|
||||||
showAlert("Please choose a file to upload.", "error");
|
showAlert("Please choose a file to upload.", "error");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prepare FormData for file upload
|
||||||
|
const formData = new FormData();
|
||||||
|
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
|
||||||
|
|
||||||
|
const apiUrl = "https://api.upload.io/v1/files"; // Correct API endpoint
|
||||||
|
const apiKey = "public_G22nhgTDm1B4ZvL1ia7nmepwHaYL"; // Replace with your Upload.io API key
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const formData = new FormData();
|
// Perform the file upload request to Upload.io
|
||||||
formData.append("file", file);
|
|
||||||
formData.append("expiry", expiry);
|
|
||||||
formData.append("expiryTime", timeValue);
|
|
||||||
|
|
||||||
// Replace with your actual Upload.io API URL and API key
|
|
||||||
const apiUrl = "https://api.upload.io/v1/files"; // Placeholder API URL
|
|
||||||
const apiKey = "public_G22nhgTDm1B4ZvL1ia7nmepwHaYL"; // Replace with your Upload.io API key
|
|
||||||
|
|
||||||
const response = await fetch(apiUrl, {
|
const response = await fetch(apiUrl, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Authorization": `Bearer ${apiKey}`,
|
"Authorization": `Bearer ${apiKey}`, // Your Upload.io API key for authentication
|
||||||
},
|
},
|
||||||
body: formData,
|
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();
|
const data = await response.json();
|
||||||
|
|
||||||
if (data && data.url) {
|
// Handle the response
|
||||||
|
if (response.ok && data.url) {
|
||||||
const fileLink = document.getElementById("fileLink");
|
const fileLink = document.getElementById("fileLink");
|
||||||
fileLink.href = data.url; // Assuming the response contains the file URL
|
fileLink.href = data.url; // Assuming the response contains the file URL
|
||||||
fileLink.textContent = data.url;
|
fileLink.textContent = data.url;
|
||||||
|
|
||||||
document.getElementById("result").style.display = "block";
|
document.getElementById("result").style.display = "block";
|
||||||
showAlert("Success!", "success");
|
showAlert("File uploaded successfully! See the link below.", "success");
|
||||||
} else {
|
} 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) {
|
} catch (error) {
|
||||||
console.error("Upload failed:", error);
|
console.error("Error:", error);
|
||||||
showAlert("Failed to upload.", "error");
|
showAlert("Failed to upload file. Check your internet or try again later.", "error");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue