admin管理员组文章数量:1433160
I'm trying to use bootstrap-fileupload.js:
.html#fileupload
But I'm not sure how to use the uploaded image and the remove option. For example: I want to replace an image with the new uploaded one.
The image to be replaced:
<img class="background" src="img/background.png" style="position: absolute; top: 190px; left: 138px;"/>
From my understanding, something like this should be done with JQuery:
$('img.background').attr('src','different/path/to/my/image.jpg');
But in my case, it's an uploaded picture - How do I get its path? And how do I use the remove file option? I assume it's something like this?
// if the file is removed
$('img.background').attr('src','img/background.png');
I'm trying to use bootstrap-fileupload.js:
http://jasny.github.io/bootstrap/javascript.html#fileupload
But I'm not sure how to use the uploaded image and the remove option. For example: I want to replace an image with the new uploaded one.
The image to be replaced:
<img class="background" src="img/background.png" style="position: absolute; top: 190px; left: 138px;"/>
From my understanding, something like this should be done with JQuery:
$('img.background').attr('src','different/path/to/my/image.jpg');
But in my case, it's an uploaded picture - How do I get its path? And how do I use the remove file option? I assume it's something like this?
// if the file is removed
$('img.background').attr('src','img/background.png');
Share
Improve this question
asked Sep 9, 2013 at 20:26
user2653179user2653179
4231 gold badge8 silver badges22 bronze badges
1
- You need to know the path where the uploaded file is stored in relation to the web root. – Diodeus - James MacFarlane Commented Sep 9, 2013 at 20:28
1 Answer
Reset to default 6DEMO HERE
EDIT: I added a button remove uploaded file as you want,if you don't like show/hide effect just delete slow
, Image is shown only on upload:
$('#blah').hide();
$('#remove').hide();
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#imgInp").change(function(){
if( $('#imgInp').val()!=""){
$('#remove').show();
$('#blah').show('slow');
}
else{ $('#remove').hide();$('#blah').hide('slow');}
readURL(this);
});
$('#remove').click(function(){
$('#imgInp').val('');
$(this).hide();
$('#blah').hide('slow');
$('#blah').attr('src','http://upload.wikimedia/wikipedia/mons/thumb/4/40/No_pub.svg/150px-No_pub.svg.png');
});
This s an example made by someone( Not me :) will help you i suppose , see the code below:
function readPath(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#imgInp").change(function(){
readPath(this);
});
HTML Code:
<form id="form1">
<input type='file' id="imgInp" />
<img id="blah" src="#" alt="your image" />
</form>
本文标签: javascriptReplace an image with an uploaded image (BootstrapjQuery)Stack Overflow
版权声明:本文标题:javascript - Replace an image with an uploaded image (Bootstrap, JQuery) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744376439a2603283.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论