Simple Image Preview Before Upload
Submitted by rinvizle on Tuesday, August 30, 2016 - 16:22.
In this tutorial we will show you how to create a Simple Image Preview Before Upload. This feature lets the user view the chosen image before they upload it. And you can easily add image preview option on file upload using JavaScript and jQuery. Simple Image Preview Before Upload is a most required feature for file upload functionality. It helps the user to view chosen file and change the image before upload. From the user perspective, it is very helpful to uploading perfect image or file without doing the repetitive upload.
JavaScript File Reader - it will be used to read the content of the file in file preview function. Once the file content is loaded we’ll render the image preview under the file upload form.
Index.html - This is for the GUI of the web page to make it more responsive and attractive.
Hope that you learn in my tutorial. Don't forget to LIKE & SHARE this website. Enjoy Coding.
Sample Code
Upload.php - This script is use to upload the file to the respective directory when the user clicked on submit button.- <?php
- echo 'File has uploaded successfully.';
- }else{
- echo 'Some problem occurred, please try again.';
- }
- }
- ?>
- function filePreview(input) {
- if (input.files && input.files[0]) {
- var reader = new FileReader();
- reader.onload = function (e) {
- $('#uploadForm + img').remove();
- $('#uploadForm').after('<img src="'+e.target.result+'" width="450" height="300"/>');
- }
- reader.readAsDataURL(input.files[0]);
- }
- }
- $("#file").change(function () {
- filePreview(this);
- });
- <!DOCTYPE html>
- <html>
- <head>
- <link rel="stylesheet" type="text/css" media="screen" href="css/style.css" />
- <link rel="stylesheet" type="text/css" media="screen" href="bootstrap/css/bootstrap.min.css" />
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- <div class="navbar-header">
- </div>
- </nav>
- <div class="container-fluid">
- <form method="post" action="upload.php" enctype="multipart/form-data" id="uploadForm">
- <div class="button">
- <input type="file" name="file" id="file" />
- <input type="submit" class="btn btn-default" name="submit" value="Upload" style="margin-left: 385px;margin-top: -25px;"/>
- </div>
- </form>
- </div>
- </body>
- </html>
Comments
Add new comment
- Add new comment
- 247 views