admin管理员组文章数量:1336725
- When I upload the image can you tell me how to show image within the red box
- Providing code below
<div class="upload-image">
<div class="upload-image-preview"></div>
<input type="file" name="file" value="Upload Image" />
</div>
- When I upload the image can you tell me how to show image within the red box
- Providing code below
http://codepen.io/anon/pen/ZWXmpd
<div class="upload-image">
<div class="upload-image-preview"></div>
<input type="file" name="file" value="Upload Image" />
</div>
Share
Improve this question
edited Apr 3, 2016 at 7:23
Ibrahim Khan
20.8k7 gold badges45 silver badges56 bronze badges
asked Apr 3, 2016 at 6:52
user6015171user6015171
3 Answers
Reset to default 5You can use something like this to display your image before upload.
var _URL = window.URL || window.webkitURL;
$("#file").change(function(e) {
var image, file;
if ((file = this.files[0])) {
image = new Image();
image.onload = function() {
src = this.src;
$('#uploadPreview').html('<img src="'+ src +'"></div>');
e.preventDefault();
}
};
image.src = _URL.createObjectURL(file);
});
#uploadPreview {
border: 1px solid red;
width: 200px;
height: 200px;
overflow: hidden;
}
#uploadPreview img {
min-width: 100%;
min-height: 100%;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" id="file"/>
<div id="uploadPreview">
</div>
You can do this using following jQuery.
$("input[name=file]").change(function () {
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
var img = $('<img>').attr('src', e.target.result);
$('.upload-image-preview').html(img);
};
reader.readAsDataURL(this.files[0]);
}
});
Use following CSS
to keep image size same as the preview div.
.upload-image-preview img{
width: 100%;
height: 100%;
}
DEMO
After uploading the image you can return the path of image. And by js you can show that image on upload response via ajax call.
本文标签: javascriptshowing uploaded image within divStack Overflow
版权声明:本文标题:javascript - showing uploaded image within div - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742381037a2464095.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论