[javascript] Preview an image before it is uploaded

I want to be able to preview a file (image) before it is uploaded. The preview action should be executed all in the browser without using Ajax to upload the image.

How can I do this?

This question is related to javascript jquery file-upload

The answer is


Default Iamge

                @Html.TextBoxFor(x => x.productModels.DefaultImage, new {@type = "file", @class = "form-control", onchange = "openFile(event)", @name = "DefaultImage", @id = "DefaultImage" })
                @Html.ValidationMessageFor(model => model.productModels.DefaultImage, "", new { @class = "text-danger" })
                    <img src="~/img/ApHandler.png"  style="height:125px; width:125px" id="DefaultImagePreview"/>
            </div>

 <script>
    var openFile = function (event) {
        var input = event.target;

        var reader = new FileReader();
        reader.onload = function () {
            var dataURL = reader.result;
            var output = document.getElementById('DefaultImagePreview');
            output.src = dataURL;
        };
        reader.readAsDataURL(input.files[0]);
    };
</script>

Here's a multiple files version, based on Ivan Baev's answer.

The HTML

<input type="file" multiple id="gallery-photo-add">
<div class="gallery"></div>

JavaScript / jQuery

$(function() {
    // Multiple images preview in browser
    var imagesPreview = function(input, placeToInsertImagePreview) {

        if (input.files) {
            var filesAmount = input.files.length;

            for (i = 0; i < filesAmount; i++) {
                var reader = new FileReader();

                reader.onload = function(event) {
                    $($.parseHTML('<img>')).attr('src', event.target.result).appendTo(placeToInsertImagePreview);
                }

                reader.readAsDataURL(input.files[i]);
            }
        }

    };

    $('#gallery-photo-add').on('change', function() {
        imagesPreview(this, 'div.gallery');
    });
});

Requires jQuery 1.8 due to the usage of $.parseHTML, which should help with XSS mitigation.

This will work out of the box, and the only dependancy you need is jQuery.


How about creating a function that loads the file and fires a custom event. Then attach a listener to the input. This way we have more flexibility to use the file, not just for previewing images.

/**
 * @param {domElement} input - The input element
 * @param {string} typeData - The type of data to be return in the event object. 
 */
function loadFileFromInput(input,typeData) {
    var reader,
        fileLoadedEvent,
        files = input.files;

    if (files && files[0]) {
        reader = new FileReader();

        reader.onload = function (e) {
            fileLoadedEvent = new CustomEvent('fileLoaded',{
                detail:{
                    data:reader.result,
                    file:files[0]  
                },
                bubbles:true,
                cancelable:true
            });
            input.dispatchEvent(fileLoadedEvent);
        }
        switch(typeData) {
            case 'arraybuffer':
                reader.readAsArrayBuffer(files[0]);
                break;
            case 'dataurl':
                reader.readAsDataURL(files[0]);
                break;
            case 'binarystring':
                reader.readAsBinaryString(files[0]);
                break;
            case 'text':
                reader.readAsText(files[0]);
                break;
        }
    }
}
function fileHandler (e) {
    var data = e.detail.data,
        fileInfo = e.detail.file;

    img.src = data;
}
var input = document.getElementById('inputId'),
    img = document.getElementById('imgId');

input.onchange = function (e) {
    loadFileFromInput(e.target,'dataurl');
};

input.addEventListener('fileLoaded',fileHandler)

Probably my code isn't as good as some users but I think you will get the point of it. Here you can see an example


<img id="blah" alt="your image" width="100" height="100" />
<input type="file" name="photo" id="fileinput" />
<script>
$('#fileinput').change(function() {
var url = window.URL.createObjectURL(this.files[0]);
 $('#blah').attr('src',url);
});
</script>

For Multiple image upload (Modification to the @IvanBaev's Solution)

function readURL(input) {
    if (input.files && input.files[0]) {
        var i;
        for (i = 0; i < input.files.length; ++i) {
          var reader = new FileReader();
          reader.onload = function (e) {
              $('#form1').append('<img src="'+e.target.result+'">');
          }
          reader.readAsDataURL(input.files[i]);
        }
    }
}

http://jsfiddle.net/LvsYc/12330/

Hope this helps someone.


TO PREVIEW MULTIPLE FILES using jquery

_x000D_
_x000D_
$(document).ready(function(){
    $('#image').change(function(){
        $("#frames").html('');
        for (var i = 0; i < $(this)[0].files.length; i++) {
            $("#frames").append('<img src="'+window.URL.createObjectURL(this.files[i])+'" width="100px" height="100px"/>');
        }
    });
});
_x000D_
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
    <input type="file" id="image" name="image[]" multiple /><br/>
    <div id="frames"></div>
</body>
_x000D_
_x000D_
_x000D_


Preview multiple images before it is uploaded using jQuery/javascript?

This will preview multiple files as thumbnail images at a time

Html

<input id="ImageMedias" multiple="multiple" name="ImageMedias" type="file"
accept=".jfif,.jpg,.jpeg,.png,.gif" class="custom-file-input"  value="">                                    
<div id="divImageMediaPreview"></div>

Script

$("#ImageMedias").change(function () {
    if (typeof (FileReader) != "undefined") {
        var dvPreview = $("#divImageMediaPreview");
        dvPreview.html("");            
        $($(this)[0].files).each(function () {
            var file = $(this);                
                var reader = new FileReader();
                reader.onload = function (e) {
                    var img = $("<img />");
                    img.attr("style", "width: 150px; height:100px; padding: 10px");
                    img.attr("src", e.target.result);
                    dvPreview.append(img);
                }
                reader.readAsDataURL(file[0]);                
        });
    } else {
        alert("This browser does not support HTML5 FileReader.");
    }
});

Working Demo on Codepen

Working Demo on jsfiddle

I hope this will help.


This is size improvement of cmlevy answer - try

_x000D_
_x000D_
<input type=file oninput="pic.src=window.URL.createObjectURL(this.files[0])">
<img id="pic" />
_x000D_
_x000D_
_x000D_


function assignFilePreviews() {
    $('input[data-previewable=\"true\"]').change(function() {
        var prvCnt = $(this).attr('data-preview-container');
        if (prvCnt) {
            if (this.files && this.files[0]) {
                var reader = new FileReader();
                reader.onload = function(e) {
                    var img = $('<img>');
                    img.attr('src', e.target.result);
                    img.error(function() {
                        $(prvCnt).html('');
                    });
                    $(prvCnt).html('');
                    img.appendTo(prvCnt);
                }
                reader.readAsDataURL(this.files[0]);
            }
        }
    });
}
$(document).ready(function() {
    assignFilePreviews();
});

HTML

<input type="file" data-previewable="true" data-preview-container=".prd-img-prv" />
<div class = "prd-img-prv"></div>

This also handles case when file with invalid type ( ex. pdf ) is choosen


Here's a solution if you're using React:

import * as React from 'react'
import { useDropzone } from 'react-dropzone'

function imageDropper() {
  const [imageUrl, setImageUrl] = React.useState()
  const [imageFile, setImageFile] = React.useState()

  const onDrop = React.useCallback(
    acceptedFiles => {
      const file = acceptedFiles[0]
      setImageFile(file)

      // convert file to data: url
      const reader = new FileReader()
      reader.addEventListener('load', () => setImageUrl(String(reader.result)), false)
      reader.readAsDataURL(file)
    },
    [setImageFile, setImageUrl]
  )
  const { getRootProps, getInputProps, isDragActive } = useDropzone({ onDrop })

  return (
    <div>
      <div {...getRootProps()}>
        {imageFile ? imageFile.name : ''}
        {isDragActive ? <p>Drop files here...</p> : <p>Select image file...</p>}
        <input {...getInputProps()} />
      </div>
      {imageUrl && (
        <div>
          Your image: <img src={imageUrl} />
        </div>
      )}
    </div>
  )
}

THIS IS THE SIMPLEST METHOD


To PREVIEW the image before uploading it to the SERVER from the Browser without using Ajax or any complicated functions.


It needs an "onChange" event to load the image.

_x000D_
_x000D_
function preview() {
    frame.src=URL.createObjectURL(event.target.files[0]);
}
_x000D_
<form>
  <input type="file" onchange="preview()">
  <img id="frame" src="" width="100px" height="100px"/>
</form>
_x000D_
_x000D_
_x000D_

To preview multiple image click here


It's my code.Support IE[6-9]?chrome 17+?firefox?Opera 11+?Maxthon3

_x000D_
_x000D_
   _x000D_
function previewImage(fileObj, imgPreviewId) {_x000D_
    var allowExtention = ".jpg,.bmp,.gif,.png";  //allowed to upload file type_x000D_
    document.getElementById("hfAllowPicSuffix").value;_x000D_
    var extention = fileObj.value.substring(fileObj.value.lastIndexOf(".") + 1).toLowerCase();_x000D_
    var browserVersion = window.navigator.userAgent.toUpperCase();_x000D_
    if (allowExtention.indexOf(extention) > -1) {_x000D_
        if (fileObj.files) {_x000D_
            if (window.FileReader) {_x000D_
                var reader = new FileReader();_x000D_
                reader.onload = function (e) {_x000D_
                    document.getElementById(imgPreviewId).setAttribute("src", e.target.result);_x000D_
                };_x000D_
                reader.readAsDataURL(fileObj.files[0]);_x000D_
            } else if (browserVersion.indexOf("SAFARI") > -1) {_x000D_
                alert("don't support  Safari6.0 below broswer");_x000D_
            }_x000D_
        } else if (browserVersion.indexOf("MSIE") > -1) {_x000D_
            if (browserVersion.indexOf("MSIE 6") > -1) {//ie6_x000D_
                document.getElementById(imgPreviewId).setAttribute("src", fileObj.value);_x000D_
            } else {//ie[7-9]_x000D_
                fileObj.select();_x000D_
                fileObj.blur(); _x000D_
                var newPreview = document.getElementById(imgPreviewId);_x000D_
_x000D_
                newPreview.style.border = "solid 1px #eeeeee";_x000D_
                newPreview.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='scale',src='" + document.selection.createRange().text + "')";_x000D_
                newPreview.style.display = "block";_x000D_
_x000D_
            }_x000D_
        } else if (browserVersion.indexOf("FIREFOX") > -1) {//firefox_x000D_
            var firefoxVersion = parseFloat(browserVersion.toLowerCase().match(/firefox\/([\d.]+)/)[1]);_x000D_
            if (firefoxVersion < 7) {//firefox7 below_x000D_
                document.getElementById(imgPreviewId).setAttribute("src", fileObj.files[0].getAsDataURL());_x000D_
            } else {//firefox7.0+ _x000D_
                document.getElementById(imgPreviewId).setAttribute("src", window.URL.createObjectURL(fileObj.files[0]));_x000D_
            }_x000D_
        } else {_x000D_
            document.getElementById(imgPreviewId).setAttribute("src", fileObj.value);_x000D_
        }_x000D_
    } else {_x000D_
        alert("only support" + allowExtention + "suffix");_x000D_
        fileObj.value = ""; //clear Selected file_x000D_
        if (browserVersion.indexOf("MSIE") > -1) {_x000D_
            fileObj.select();_x000D_
            document.selection.clear();_x000D_
        }_x000D_
_x000D_
    }_x000D_
}_x000D_
function changeFile(elem) {_x000D_
    //file object , preview img tag id_x000D_
    previewImage(elem,'imagePreview')_x000D_
}
_x000D_
<input type="file"  id="netBarBig"  onchange="changeFile(this)"  />_x000D_
<img  src="" id="imagePreview" style="width:120px;height:80px;" alt=""/>
_x000D_
_x000D_
_x000D_


Clean and simple JSfiddle

This will be useful when you want The event to triggered indirectly from a div or a button.

<img id="image-preview"  style="height:100px; width:100px;"  src="" >

<input style="display:none" id="input-image-hidden" onchange="document.getElementById('image-preview').src = window.URL.createObjectURL(this.files[0])" type="file" accept="image/jpeg, image/png">

<button  onclick="HandleBrowseClick('input-image-hidden');" >UPLOAD IMAGE</button>


<script type="text/javascript">
function HandleBrowseClick(hidden_input_image)
{
    var fileinputElement = document.getElementById(hidden_input_image);
    fileinputElement.click();
}     
</script>

What about this solution?

Just add the data attribute "data-type=editable" to an image tag like this:

<img data-type="editable" id="companyLogo" src="http://www.coventrywebgraphicdesign.co.uk/wp-content/uploads/logo-here.jpg" height="300px" width="300px" />

And the script to your project off course...

function init() {
    $("img[data-type=editable]").each(function (i, e) {
        var _inputFile = $('<input/>')
            .attr('type', 'file')
            .attr('hidden', 'hidden')
            .attr('onchange', 'readImage()')
            .attr('data-image-placeholder', e.id);

        $(e.parentElement).append(_inputFile);

        $(e).on("click", _inputFile, triggerClick);
    });
}

function triggerClick(e) {
    e.data.click();
}

Element.prototype.readImage = function () {
    var _inputFile = this;
    if (_inputFile && _inputFile.files && _inputFile.files[0]) {
        var _fileReader = new FileReader();
        _fileReader.onload = function (e) {
            var _imagePlaceholder = _inputFile.attributes.getNamedItem("data-image-placeholder").value;
            var _img = $("#" + _imagePlaceholder);
            _img.attr("src", e.target.result);
        };
        _fileReader.readAsDataURL(_inputFile.files[0]);
    }
};

// 
// IIFE - Immediately Invoked Function Expression
// https://stackoverflow.com/questions/18307078/jquery-best-practises-in-case-of-document-ready
(

function (yourcode) {
    "use strict";
    // The global jQuery object is passed as a parameter
    yourcode(window.jQuery, window, document);
}(

function ($, window, document) {
    "use strict";
    // The $ is now locally scoped 
    $(function () {
        // The DOM is ready!
        init();
    });

    // The rest of your code goes here!
}));

See demo at JSFiddle


Try this

_x000D_
_x000D_
window.onload = function() {_x000D_
  if (window.File && window.FileList && window.FileReader) {_x000D_
    var filesInput = document.getElementById("uploadImage");_x000D_
    filesInput.addEventListener("change", function(event) {_x000D_
      var files = event.target.files;_x000D_
      var output = document.getElementById("result");_x000D_
      for (var i = 0; i < files.length; i++) {_x000D_
        var file = files[i];_x000D_
        if (!file.type.match('image'))_x000D_
          continue;_x000D_
        var picReader = new FileReader();_x000D_
        picReader.addEventListener("load", function(event) {_x000D_
          var picFile = event.target;_x000D_
          var div = document.createElement("div");_x000D_
          div.innerHTML = "<img class='thumbnail' src='" + picFile.result + "'" +_x000D_
            "title='" + picFile.name + "'/>";_x000D_
          output.insertBefore(div, null);_x000D_
        });        _x000D_
        picReader.readAsDataURL(file);_x000D_
      }_x000D_
_x000D_
    });_x000D_
  }_x000D_
}
_x000D_
<input type="file" id="uploadImage" name="termek_file" class="file_input" multiple/>_x000D_
<div id="result" class="uploadPreview">
_x000D_
_x000D_
_x000D_


I have edited @Ivan's answer to display "No Preview Available" image, if it is not an image:

function readURL(input) {
    var url = input.value;
    var ext = url.substring(url.lastIndexOf('.') + 1).toLowerCase();
    if (input.files && input.files[0]&& (ext == "gif" || ext == "png" || ext == "jpeg" || ext == "jpg")) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $('.imagepreview').attr('src', e.target.result);
        }

        reader.readAsDataURL(input.files[0]);
    }else{
         $('.imagepreview').attr('src', '/assets/no_preview.png');
    }
}

There are a couple ways you can do this. The most efficient way would be to use URL.createObjectURL() on the File from your <input>. Pass this URL to img.src to tell the browser to load the provided image.

Here's an example:

_x000D_
_x000D_
<input type="file" accept="image/*" onchange="loadFile(event)">_x000D_
<img id="output"/>_x000D_
<script>_x000D_
  var loadFile = function(event) {_x000D_
    var output = document.getElementById('output');_x000D_
    output.src = URL.createObjectURL(event.target.files[0]);_x000D_
    output.onload = function() {_x000D_
      URL.revokeObjectURL(output.src) // free memory_x000D_
    }_x000D_
  };_x000D_
</script>
_x000D_
_x000D_
_x000D_

You can also use FileReader.readAsDataURL() to parse the file from your <input>. This will create a string in memory containing a base64 representation of the image.

_x000D_
_x000D_
<input type="file" accept="image/*" onchange="loadFile(event)">_x000D_
<img id="output"/>_x000D_
<script>_x000D_
  var loadFile = function(event) {_x000D_
    var reader = new FileReader();_x000D_
    reader.onload = function(){_x000D_
      var output = document.getElementById('output');_x000D_
      output.src = reader.result;_x000D_
    };_x000D_
    reader.readAsDataURL(event.target.files[0]);_x000D_
  };_x000D_
</script>
_x000D_
_x000D_
_x000D_


Yes. It is possible.

Html

<input type="file" accept="image/*"  onchange="showMyImage(this)" />
 <br/>
<img id="thumbnil" style="width:20%; margin-top:10px;"  src="" alt="image"/>

JS

 function showMyImage(fileInput) {
        var files = fileInput.files;
        for (var i = 0; i < files.length; i++) {           
            var file = files[i];
            var imageType = /image.*/;     
            if (!file.type.match(imageType)) {
                continue;
            }           
            var img=document.getElementById("thumbnil");            
            img.file = file;    
            var reader = new FileReader();
            reader.onload = (function(aImg) { 
                return function(e) { 
                    aImg.src = e.target.result; 
                }; 
            })(img);
            reader.readAsDataURL(file);
        }    
    }

You can get Live Demo from here.


for my app, with encryped GET url parameters, only this worked. I always got a TypeError: $(...) is null. Taken from https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL

_x000D_
_x000D_
function previewFile() {_x000D_
  var preview = document.querySelector('img');_x000D_
  var file    = document.querySelector('input[type=file]').files[0];_x000D_
  var reader  = new FileReader();_x000D_
_x000D_
  reader.addEventListener("load", function () {_x000D_
    preview.src = reader.result;_x000D_
  }, false);_x000D_
_x000D_
  if (file) {_x000D_
    reader.readAsDataURL(file);_x000D_
  }_x000D_
}
_x000D_
<input type="file" onchange="previewFile()"><br>_x000D_
<img src="" height="200" alt="Image preview...">
_x000D_
_x000D_
_x000D_


Example with multiple images using JavaScript (jQuery) and HTML5

JavaScript (jQuery)

function readURL(input) {
     for(var i =0; i< input.files.length; i++){
         if (input.files[i]) {
            var reader = new FileReader();

            reader.onload = function (e) {
               var img = $('<img id="dynamic">');
               img.attr('src', e.target.result);
               img.appendTo('#form1');  
            }
            reader.readAsDataURL(input.files[i]);
           }
        }
    }

    $("#imgUpload").change(function(){
        readURL(this);
    });
}

Markup (HTML)

<form id="form1" runat="server">
    <input type="file" id="imgUpload" multiple/>
</form>

Following is the working code.

<input type='file' onchange="readURL(this);" /> 
<img id="ShowImage" src="#" />

Javascript:

 function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('#ShowImage')
                    .attr('src', e.target.result)
                    .width(150)
                    .height(200);
            };

            reader.readAsDataURL(input.files[0]);
        }
    }

I have made a plugin which can generate the preview effect in IE 7+ thanks to the internet, but has few limitations. I put it into a github page so that its easier to get it

_x000D_
_x000D_
$(function () {_x000D_
  $("input[name=file1]").previewimage({_x000D_
   div: ".preview",_x000D_
   imgwidth: 180,_x000D_
   imgheight: 120_x000D_
  });_x000D_
  $("input[name=file2]").previewimage({_x000D_
   div: ".preview2",_x000D_
   imgwidth: 90,_x000D_
   imgheight: 90_x000D_
  });_x000D_
 });
_x000D_
.preview > div {_x000D_
  display: inline-block;_x000D_
  text-align:center;_x000D_
}_x000D_
_x000D_
.preview2 > div {_x000D_
  display: inline-block; _x000D_
  text-align:center;_x000D_
}
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>_x000D_
<script src="https://rawgit.com/andrewng330/PreviewImage/master/preview.image.min.js"></script>_x000D_
 Preview_x000D_
 <div class="preview"></div>_x000D_
 Preview2_x000D_
 <div class="preview2"></div>_x000D_
_x000D_
 <form action="#" method="POST" enctype="multipart/form-data">_x000D_
  <input type="file" name="file1">_x000D_
  <input type="file" name="file2">_x000D_
  <input type="submit">_x000D_
 </form>
_x000D_
_x000D_
_x000D_


https://stackoverflow.com/a/59985954/8784402

ES2017 Way

_x000D_
_x000D_
// convert file to a base64 url
const readURL = file => {
    return new Promise((res, rej) => {
        const reader = new FileReader();
        reader.onload = e => res(e.target.result);
        reader.onerror = e => rej(e);
        reader.readAsDataURL(file);
    });
};

// for demo
const fileInput = document.createElement('input');
fileInput.type = 'file';
const img = document.createElement('img');
img.attributeStyleMap.set('max-width', '320px');
document.body.appendChild(fileInput);
document.body.appendChild(img);

const preview = async event => {
    const file = event.target.files[0];
    const url = await readURL(file);
    img.src = url;
};

fileInput.addEventListener('change', preview);
_x000D_
_x000D_
_x000D_


In React, if the file is in your props, you can use:

{props.value instanceof File && (
    <img src={URL.createObjectURL(props.value)}/>
)}

The answer of LeassTaTT works well in "standard" browsers like FF and Chrome. The solution for IE exists but looks different. Here description of cross-browser solution:

In HTML we need two preview elements, img for standard browsers and div for IE

HTML:

<img id="preview" 
     src="" 
     alt="" 
     style="display:none; max-width: 160px; max-height: 120px; border: none;"/>

<div id="preview_ie"></div>

In CSS we specify the following IE specific thing:

CSS:

#preview_ie {
  FILTER: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)
}  

In HTML we include the standard and the IE-specific Javascripts:

<script type="text/javascript">
  {% include "pic_preview.js" %}
</script>  
<!--[if gte IE 7]> 
<script type="text/javascript">
  {% include "pic_preview_ie.js" %}
</script>

The pic_preview.js is the Javascript from the LeassTaTT's answer. Replace the $('#blah') whith the $('#preview') and add the $('#preview').show()

Now the IE specific Javascript (pic_preview_ie.js):

function readURL (imgFile) {    
  var newPreview = document.getElementById('preview_ie');
  newPreview.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = imgFile.value;
  newPreview.style.width = '160px';
  newPreview.style.height = '120px';
}    

That's is. Works in IE7, IE8, FF and Chrome. Please test in IE9 and report. The idea of IE preview was found here: http://forums.asp.net/t/1320559.aspx

http://msdn.microsoft.com/en-us/library/ms532969(v=vs.85).aspx


One-liner solution:

The following code uses object URLs, which is much more efficient than data URL for viewing large images (A data URL is a huge string containing all of the file data, whereas an object URL, is just a short string referencing the file data in-memory):

_x000D_
_x000D_
<img id="blah" alt="your image" width="100" height="100" />_x000D_
_x000D_
<input type="file" _x000D_
    onchange="document.getElementById('blah').src = window.URL.createObjectURL(this.files[0])">
_x000D_
_x000D_
_x000D_

Generated URL will be like:

blob:http%3A//localhost/7514bc74-65d4-4cf0-a0df-3de016824345

Here is a much easy way to preview image before upload using pure javascript;

//profile_change is the id of the input field where we choose an image
document.getElementById("profile_change").addEventListener("change", function() {

//Here we select the first file among the selected files.
const file = this.files[0];

/*here i used a label for the input field which is an image and this image will 
  represent the photo selected and profile_label is the id of this label */
const profile_label = document.getElementById("profile_label");

//Here we check if a file is selected
if(file) {
    //Here we bring in the FileReader which reads the file info. 
    const reader = new FileReader();
    
    /*After reader loads we change the src attribute of the label to the url of the 
    new image selected*/
    reader.addEventListener("load", function() {
        dp_label.setAttribute("src", this.result);
    })

    /*Here we are reading the file as a url i.e, we try to get the location of the 
    file to set that as the src of the label which we did above*/
    reader.readAsDataURL(file);

}else {
    //Here we simply set the src as default, whatever you want if no file is selected.
    dp_label.setAttribute("src", "as_you_want")
}
});

And here is the HTML;

<label for="profile_change">
            <img title="Change Profile Photo" id="profile_label" 
             src="as_you_want" alt="DP" style="height: 150px; width: 150px; 
               border-radius: 50%;" >
</label>
<input style="display: none;" id="profile_change" name="DP" type="file" class="detail form-control">

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to file-upload

bootstrap 4 file input doesn't show the file name How to post a file from a form with Axios File Upload In Angular? How to set the max size of upload file The request was rejected because no multipart boundary was found in springboot Send multipart/form-data files with angular using $http File upload from <input type="file"> How to upload files in asp.net core? REST API - file (ie images) processing - best practices Angular - POST uploaded file