Hello I'm writting my own boilerplate with express, and js framework for front-end(Angular2) I want to include profile picture options.
So now, from the input file i encode to base 64 the picture, i send the base 64 image to the server, i decode base 64 image and store it as a picture.
For displaying image to the client, i was thinking to encoded image stored on server to base 64 and sending it to front-end and display it under img src=""
It is good or bad implementation ?
For bigger file (video, files), what did you recommend ?
Thank's
Theres no need for all the base64 unless your storing the photo in an sql database. Sending the image to the server should be fine without it, storing the image on the server as a file should be fine without it, and sending the image back to the browser should be fine... without it.
You say these are profile pictures. Rename the picture to something unique - say, profileimageuserid.ext and ideally, upload the image to a CDN.
Same with the videos / files. None of these need to be Base64 encoded if their just being stored as regular files on a server or CDN.
After years of doing this common feature needed in every app today I've written a small Image utility object (which allows you to download images as Blob, convert images to base64, canvas, etc, resize image) and came up with this simple algorithm:
<input type="file">to file uploadsinput._fileproperty to store a Blob.input._file.input._fileinput._file = blobinput._filehas higher priority then the original. If it is set, then upload it or input.files[0] instead which is an instance of Blob interface also.Sample code:
import { BunnyImage } from 'bunnyjs/src/file/image'; const imageURL = '...'; const fileInput = document.getElementById('photo_upload'); const form = document.getElementById('photo_form'); BunnyImage.getImage(imageURL).then(image => { return BunnyImage.resizeImage(image, 800, 600); }).then(image => { fileInput._file = BunnyImage.imageToBlob(image); }); form.addEventListener('submit', e => { e.preventDefault(); const fd = new FormData; const blob = fileInput._file ? fileInput._file : fileInput.files[0]; const postName = fileInput.getAttribute('name'); fd.append(postName, blob, 'picture.jpg'); const request = new XMLHttpRequest(); request.open("POST", form.getAttribute('action')); request.send(fd); });All that algorithm soon will be a part of a new component in BunnyJS. If you liked an answer, please star project on GitHub and share it. If you will have any feedback, ideas, proposals or questions please let me know.