it needs to be only in vanilla javascript createa form that uploads a file with a button to a wbsite and is visible
Assistant
Sure, I'll show you how to create a simple form to upload a file using vanilla JavaScript. This code will create an HTML form with an input element for file selection and a button to upload the file. Additionally, it will display the uploaded file after a successful upload:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Upload</title>
</head>
<body>
<h1>Upload a File</h1>
<!-- Form to upload file -->
<form id="uploadForm">
<input type="file" id="fileInput"><br><br>
<button type="button" onclick="uploadFile()">Upload</button>
</form>
<!-- Area to display the uploaded file -->
<div id="fileDisplayArea"></div>
<script>
function uploadFile() {
// Get the file input element and its value
const fileInput = document.getElementById('fileInput');
const file = fileInput.files[0];
// Check if a file is selected
if (!file) {
alert("Please select a file to upload");
return;
}
// Create a FormData object
const formData = new FormData();
formData.append('file', file);
// Make an XMLHttpRequest to upload the file
const xhr = new XMLHttpRequest();
xhr.open('POST', 'YOUR_UPLOAD_ENDPOINT_HERE', true);
// Handling file upload response
xhr.onload = function () {
if (xhr.status === 200) {
// Parse the JSON response
const response = JSON.parse(xhr.responseText);
// Display the uploaded file or any message from response
const fileDisplayArea = document.getElementById('fileDisplayArea');
fileDisplayArea.innerHTML = `<p>File uploaded successfully: ${response.fileName}</p>`;
// Alternatively, if the server returns the file URL
// fileDisplayArea.innerHTML = `<p>File uploaded successfully: <a href="${response.fileURL}" target="_blank">${response.fileName}</a></p>`;
} else {
alert("File upload failed. Status: " + xhr.status);
}
};
// Send the FormData object
xhr.send(formData);
}
</script>
</body>
</html>
Explanation
HTML Structure:
A file input element (<input type="file" id="fileInput">) to allow users to select a file.
A button to trigger the upload function (<button type="button" onclick="uploadFile()">Upload</button>).
A <div> element to display the uploaded file or status message (<div id="fileDisplayArea"></div>).
JavaScript Function (uploadFile):
It gets the selected file from the input element.
Creates a FormData object and appends the file to it.
Uses XMLHttpRequest to send the file to a server (replace 'YOUR_UPLOAD_ENDPOINT_HERE' with your actual upload endpoint).
Handles the server response to display a message or the uploaded file details.
Note
Replace 'YOUR_UPLOAD_ENDPOINT_HERE' with the actual URL where you want to upload the file.
This example assumes that the server returns a JSON response with the uploaded file's name or URL. Adjust accordingly based on your server's response format.
Remember
This example assumes you already have a server-side endpoint to handle file uploads. If you don't have it, you need to implement it using server-side programming languages like Node.js, PHP, Python, etc.