JavaScript selecting image to an img tag

后端 未结 4 1845
余生分开走
余生分开走 2021-01-18 06:56

I have a fake profile system for a class project, it requires a profile picture, and it needs an option to change it locally (from your hard drive). I have a working i

4条回答
  •  感情败类
    2021-01-18 07:52

    There's FileReader.readAsDataURL (see Indra's answer) and also window.URL.createObjectURL:

    function changePicture() {
        //open the open file dialog
        document.getElementById('upload').click();
        document.getElementById('upload').onchange = function() {
            var file = this.files[0];
            var url = window.URL.createObjectURL(file);
            document.getElementById('image').src = url;
        };
    }
    

提交回复
热议问题