Before diving deep into the course of the day, it is of importance you understand the following concepts.
Ok, I started off this particular study because I was stucked on a project, it was in a way very embarassing cause it took me hours to even figure out the best google search or even find the answer. I decided to document all what i gained from this particular study and share it, as it might be of immense help to you people out there.
Topic: Uploading an image through an input tag, creating an src for it and formating it.
Keywords are; DOMString, change, input, required, accept, capture, multiple, files, value.
Value: A file input value attribute contains the DOMString that represent the path to the selected file. if the user selected multiple files, the value represents the first file in the list of files they selected.
The Value if read with input.value has a prefix like C:fakepath… This saves the file structure of the user from malicious software with intentions of accessing the users content.
Format of input
<input type="file" id="inputImage" accept="image/png, image/jpg" capture="" value="src" />
type: This gives the input tag a direction and line of action which is accessing files.
id: This is used by javascript or css for accessing and manipulating the input tag.
accept: This specifies the particular kind of file type the file input should accept. Various types are seperatied with commas like the example above. the file accepts only png and jpg files.
An example of a tag that accepts word documet is
<input type="file" id="pickDocument"
accept=".doc,.docx,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document">
capture: This attribute specify what camara would be used to capture the image or video data; if accept attribute is indicated as user the front camara would be used but if it is environment, the rare camara would be used. This also works on a microphone.
multiple: This is a boolean attribute that allows user to upload more than one file at a time.
files: This is embeded in the browser and every file uploaded using the <input type="file"/> appends itself to this array.
//
var input = document.querySelector('#inputImage');
//listen out for the change activity
input.addEventListener('change', upLoadImage);
function upLoadImage() {
var file = input.files;
console.log(file)
//If you successfully logged the above in the console, it means you got an object and means that you are on the right path.
//append the div with the result
var image = document.querySelector('.image')
var newImage = document.createElement('img')
//Declare attributes of img including src
newImage.setAttribute('src', URL.createObjectURL(file[0]));
//append the Image tag into the div with class image
image.append(newImage);
var name = document.querySelector('.name')
name.append(file[0].name)
}
<label>Upload Image</label>
<input type="file" id="inputImage" accept=".png, .jpg" capture="" value="src" />
<div class="name">
<div>
<div class="image"></div>
I hope this would help you through.