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 に答える