html - JavaScript selecting image to an img tag -


I have a fake profile system for a class project, a profile picture is required for it, and changing it locally Requires an option (from your hard drive) I have a work img tag which places the placeholder image as the default, and when you click on the change image , an open file dialog opens. There is a need to work now that they are setting an image link for the img tag with the selected image, this is what I have done so far.

  & lt; Div id = "userphoto" & gt; & Lt; A & gt; & Lt; Div class = "gravatar-wrapper-128" & gt; & Lt; Img id = "image" src = "http://blog.ramboll.com/fehmarnbelt/wp-content/themes/ramboll2/images/profile-img.jpg" alt = "" class = "logo" width = "120 "Height =" 120 "& gt; & Lt; / Div & gt; & Lt; / A & gt; & Lt; Div class = "change-picture-slide" style = "top: 30px;" & Gt; & Lt; Accept input = "image / *" type = "file" id = "upload" name = "upload" style = "visibility: hidden; width: 1px; height: 1px" /> & Lt; A href = "" onclick = "transit image (); return back" & gt; Change picture & lt; / A & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt; Script & gt; Function Change Picture () {// Open Open File Dialog Document. GetElementById ('Upload'). Click (); Var link = document.getElementById ('Upload'). Url; // change photo var img = document.getElementById ("image"); Img.src = link; } & Lt; / Script & gt;  

It can not be done to know how can I choose to upload an image anonymously and how to use this link?

You can try it using Javascript in the file reader.

  & lt; Div id = "userphoto" & gt; & Lt; Div class = "gravatar-wrapper-128" & gt; & Lt; Img id = "image" src = "body-img.jpg" alt = "class =" logo "width =" 120 "height =" 120 "& gt; & Lt; / Div & gt; & Lt; Div class = "change-picture-slide" style = "top: 30px;" & Gt; & Lt; Accept input = "image / *" type = "file" id = "upload" name = "upload" onchange = "readURL (this);" Style = "visibility: hidden;" / & Gt; & Lt; A href = "" onclick = "transit image (); return back" & gt; Change picture & lt; / A & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt; Script & gt; Function Change Image () {$ ('# Upload'). Click (); } FunctionURL (Input) {if (input.files and input.files [0]) {var reader = new FileReader (); Reader.oadload = function (e) {$ ('# image') .attr ('src', e.target.result); }; Reader.readAsDataURL (input.files [0]); }} & Lt; / Script & gt;  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -