File sharing application by using JavaScript

creating a file sharing application using javascript alone is not sufficient, as it requires server-side functionality to handle file uploads, storage, and downloads. however, i can provide you with an example that demonstrates the client-side functionality for file upload using javascript and html. you will need to integrate it with a server-side component to complete the file sharing application.

Html code:
<!doctype html>
<html>
<head>
    <title>file sharing</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            font-family: arial, sans-serif;
        }

        #file-input {
            margin-bottom: 10px;
        }

        #upload-button {
            display: none;
        }
    </style>
</head>
<body>
    <input type="file" id="file-input" multiple>
    <button id="upload-button">upload</button>

    <script src="file-sharing.js"></script>
</body>
</html>

javascript (file-sharing.js):

javascript
document.getelementbyid('file-input').addeventlistener('change', handlefileselect);

function handlefileselect(event) {
    const files = event.target.files;
    const uploadbutton = document.getelementbyid('upload-button');

    if (files && files.length > 0) {
        uploadbutton.style.display = 'block';
        uploadbutton.addeventlistener('click', () => {
            uploadfiles(files);
        });
    } else {
        uploadbutton.style.display = 'none';
        uploadbutton.removeeventlistener('click', () => {
            uploadfiles(files);
        });
    }
}

function uploadfiles(files) {
    const formdata = new formdata();

    for (let i = 0; i < files.length; i++) {
        formdata.append('files', files[i]);
    }

    // make an ajax request or use the fetch api to send the form data to the server
    // here is an example using the fetch api

    fetch('/upload', {
        method: 'post',
        body: formdata
    })
    .then(response => {
        if (response.ok) {
            console.log('files uploaded successfully');
            // handle success response
        } else {
            console.error('file upload failed');
            // handle error response
        }
    })
    .catch(error => {
        console.error('error uploading files:', error);
        // handle error
    });
}

in this example, we have an html structure with an input element of type "file" (`<input type="file">`) and a button for uploading the selected files. the javascript code handles the file selection and triggers the upload process when the user clicks the upload button.

when the file input changes, the `handlefileselect` function is called. it checks if any files are selected, displays the upload button, and attaches an event listener to the button. if no files are selected, the upload button is hidden, and the event listener is removed.

when the upload button is clicked, the `uploadfiles` function is called. it creates a `formdata` object and appends the selected files to it. then, it sends an ajax request (or uses the fetch api) to the server, sending the form data in a post request to the "/upload" endpoint. you will need to replace "/upload" with the actual endpoint on your server that handles file uploads.

the server-side component will receive the uploaded files, process them, and handle the storage and sharing functionality. the server-side implementation depends on the technology stack you are using (e.g., node.js, php, python, etc.). you will need to write the server-side code separately to complete the file sharing application.


Output:

img


About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext