0

我正在尝试使用 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;"/>

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

$('img.background').attr('src','different/path/to/my/image.jpg');

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

// if the file is removed
$('img.background').attr('src','img/background.png');
4

1 回答 1

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');
});

这是某人制作的示例(不是我 :) 我想会对您有所帮助,请参见下面的代码:

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 代码:

<form id="form1">
    <input type='file' id="imgInp" />
    <img id="blah" src="#" alt="your image" />
</form>
于 2013-09-09T20:38:17.950 回答