admin管理员组文章数量:1392094
I wanted to know how to display a photo that the user uploads on a website using html and javascript. I tried using code I found on another stackoverflow question but it didn't work for me. Here is the code I tried in chrome, firefox and safari. Any help is appreciated
HTML:
<!DOCTYPE html>
<html>
<head>
<link class="jsbin" href=".css" rel="stylesheet" type="text/css" />
<script class="jsbin" src=".min.js"></script>
<script class="jsbin" src=".8.0/jquery-ui.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
<script src=".js"></script>
<![endif]-->
<style>
article, aside, figure, footer, header, hgroup,menu, nav, section {
display: block;
}
</style>
</head>
<body>
<input type='file' onchange="readURL(this);" />
<img id="blah" src="#" alt="your image" />
</body>
</html>
Javascript:
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah')
.attr('src', e.target.result)
.width(150)
.height(200);
};
reader.readAsDataURL(input.files[0]);
}
}
I wanted to know how to display a photo that the user uploads on a website using html and javascript. I tried using code I found on another stackoverflow question but it didn't work for me. Here is the code I tried in chrome, firefox and safari. Any help is appreciated
HTML:
<!DOCTYPE html>
<html>
<head>
<link class="jsbin" href="http://ajax.googleapis./ajax/libs/jqueryui/1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<script class="jsbin" src="http://ajax.googleapis./ajax/libs/jquery/1/jquery.min.js"></script>
<script class="jsbin" src="http://ajax.googleapis./ajax/libs/jqueryui/1.8.0/jquery-ui.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode./svn/trunk/html5.js"></script>
<![endif]-->
<style>
article, aside, figure, footer, header, hgroup,menu, nav, section {
display: block;
}
</style>
</head>
<body>
<input type='file' onchange="readURL(this);" />
<img id="blah" src="#" alt="your image" />
</body>
</html>
Javascript:
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah')
.attr('src', e.target.result)
.width(150)
.height(200);
};
reader.readAsDataURL(input.files[0]);
}
}
Share
Improve this question
edited Apr 20, 2018 at 14:02
Ignacio Ara
2,5802 gold badges27 silver badges37 bronze badges
asked Apr 20, 2018 at 13:19
rajzaveri5rajzaveri5
452 silver badges8 bronze badges
3
- The code works fine, however you have small problem which makes it not-working. It's easy to see if you debug your code. – dfsq Commented Apr 20, 2018 at 13:31
- Open the console and see if the action throws any error – jacobdo Commented Apr 20, 2018 at 13:33
- thank you for your feedback – rajzaveri5 Commented Apr 20, 2018 at 14:51
3 Answers
Reset to default 4You can simply use pure JS to get this working.
function handleImageUpload()
{
var image = document.getElementById("upload").files[0];
var reader = new FileReader();
reader.onload = function(e) {
document.getElementById("display-image").src = e.target.result;
}
reader.readAsDataURL(image);
}
<input id="upload" type="file" onChange="handleImageUpload()" />
<img id="display-image" src="" />
Please check the below code snippet, I have tested it on Google chrome Version 66.0.3359.117 (Official Build) (64-bit) and it is working fine.
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah').attr('src', e.target.result).width(150).height(200);
};
reader.readAsDataURL(input.files[0]);
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<script src="https://ajax.googleapis./ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<!--[if IE]>
<script src="http://html5shiv.googlecode./svn/trunk/html5.js"></script>
<![endif]-->
<style>
article,
aside,
figure,
footer,
header,
hgroup,
menu,
nav,
section {
display: block;
}
</style>
</head>
<body>
<input type='file' onchange="readURL(this);" />
<img id="blah" src="#" alt="your image" />
</body>
</html>
Your code is working fine, I have tested it.
I think you forgot to link the javascript to your HTML source code.
本文标签: How to display a photo uploaded by user in html and javascriptStack Overflow
版权声明:本文标题:How to display a photo uploaded by user in html and javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744780026a2624652.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论