用上传的图像替换图像(Bootstrap,JQuery)

use*_*179 0 html javascript jquery

我正在尝试使用bootstrap-fileupload.js:

http://jasny.github.io/bootstrap/javascript.html#fileupload

但我不知道如何使用上传的图像和删除选项.例如:我想用新上传的图像替换图像.

要替换的图像:

<img class="background" src="img/background.png" style="position: absolute; top: 190px; left: 138px;"/>
Run Code Online (Sandbox Code Playgroud)

根据我的理解,这样的事情应该用JQuery完成:

$('img.background').attr('src','different/path/to/my/image.jpg');
Run Code Online (Sandbox Code Playgroud)

但就我而言,这是一张上传的图片 - 我如何获得它的路径?我如何使用删除文件选项?我认为它是这样的?

// if the file is removed
$('img.background').attr('src','img/background.png');
Run Code Online (Sandbox Code Playgroud)

Cha*_*JRA 6

在这里演示

编辑:我添加了一个按钮删除上传的文件,如果你不喜欢显示/隐藏效果只是删除slow,图像只显示在上传:

$('#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.org/wikipedia/commons/thumb/4/40/No_pub.svg/150px-No_pub.svg.png');
});
Run Code Online (Sandbox Code Playgroud)

这是一个由某人(不是我:)制作的例子,对我来说会有所帮助,请看下面的代码:

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);
});
Run Code Online (Sandbox Code Playgroud)

HTML代码:

<form id="form1">
    <input type='file' id="imgInp" />
    <img id="blah" src="#" alt="your image" />
</form>
Run Code Online (Sandbox Code Playgroud)