All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

HTML File Upload Example JavaScript

Last Updated : Mar 11, 2024

HTML File Upload Example JavaScript

In this article we will show you the solution of HTML file upload example JavaScript, in an HTML page, if we upload a file, we can save the uploaded file using the JavaScript fetch API method. In JavaScript, the Fetch() API is a promise-based API.

By this, we can use the GET method, POST method, etc. In this tutorial, we are going to use the POST method.

Step By Step Guide On HTML File Upload Example JavaScript :-

At first, we will see an example to save data from an HTML file upload example JavaScript. Let us see the code first.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> html file upload example javascript </title>
    <style>
        h1 {
         font-size : larger ;
         font-weight : bolder ;
         color : rgb(113, 221, 113) ;
         text-align : center ;
        }
        h3 {
            text-align : center ;
            font-family : 'Lucida Sans' , 'Lucida Sans Regular' , 'Lucida Grande' , 'Lucida Sans Unicode' , Geneva , Verdana , sans-serif ;
        }
    </style>
</head>
<body>
    <h1> TALKERSCODE </h1>
    <h3> html file upload example javascript </h3>
    <form id="form">
        <input type="file" id="file" accept=".jpg, .png, .pdg, .doc"> <br>
        <input type="text" id="comments" placeholder="file comments"> <br>
        <button type="submit">
            upload file
        </button>
    </form>
   <script>
    document.getElementById('form').addEventListener('sumbit', function(){
        e.preventDefault() ;
        const userFile = document.getElementById('file').files[0] ;
        const userComments = document.getElementById('comments').value;
        const formData = new FormData();
        formData.append('user-file' , userFile, 'user-file.jpg')
        formData.append('user-comments' , userComments)
        fetch('https://httpbin.org/post' , {
            method: "POST",
            body: formData,
        })
        .then(res => res.json())
        .then(data => console.log(data))
        .catch(err => console.log(err)) ;
    })
   </script>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now the <head> tag is used to contain information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1> tag used to add heading here.
  7. Create a form using <form> tag with the id ‘form’
  8. Now create a <input> tag (for the file) with the id ‘file’, type ‘file’, and also mention which type file it can accept. Again create a <input> tag (for the comment) with the id ‘comment’, type ‘file’ and the placeholder to.
  9. Create a <button> with type ‘submit’
  10. Created the <style> tag to add CSS to the HTML page.
  11. To add JavaScript Create a <script> tag.
  12. By selecting id ‘form’ use Document.getElementById() with .addEventListener() tag.
  13. Into a function(), use a e.preventDefault() for prevent default.
  14. Create two constant variable for userFile and userComments with document.getElementById()
  15. By creating another variable formData for new FormData.
  16. Using .append operator.
  17. Using fetch API here.

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, HTML file upload example JavaScript.

I hope this article on HTML file upload example JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪