首页 > 解决方案 > 角度 5 的图像上传加载程序

问题描述

我想要一个角度为 5 的图像上传器,但我无法获得任何最好的,最后我找到了它,但它在 JS 中,我想在打字稿中使用它。

HTML …………

<div class="avatar-upload">
    <div class="avatar-edit">
        <input type='file' id="imageUpload" accept=".png, .jpg, .jpeg" />
        <label for="imageUpload"></label>
    </div>
    <div class="avatar-preview">
        <div id="imagePreview" style="background-image: 
       url(http://i.pravatar.cc/500?img=7);">
        </div>
    </div>
</div>

………………………………………………………………

body {
  background: whitesmoke;
  font-family: 'Open Sans', sans-serif;
}

.container {
    max-width: 960px;
    margin: 30px auto;
    padding: 20px;
}

h1 {
    font-size: 20px;
    text-align: center;
    margin: 20px 0 20px;
    small {
    display: block;
    font-size: 15px;
    padding-top: 8px;
    color: gray;
}   

.avatar-upload {
 position: relative;
 max-width: 205px;
 margin: 50px auto;

.avatar-edit {
    position: absolute;
    right: 12px;
    z-index: 1;
    top: 10px;

    input {
        display: none;

        + label {
            display: inline-block;
            width: 34px;
            height: 34px;
            margin-bottom: 0;
            border-radius: 100%;
            background: #FFFFFF;
            border: 1px solid transparent;
            box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.12);
            cursor: pointer;
            font-weight: normal;
            transition: all .2s ease-in-out;

            &:hover {
                background: #f1f1f1;
                border-color: #d6d6d6;
            }

            &:after {
                content: "\f040";
                font-family: 'FontAwesome';
                color: #757575;
                position: absolute;
                top: 10px;
                left: 0;
                right: 0;
                text-align: center;
                margin: auto;
            }
        }
    }
}
.avatar-preview {
    width: 192px;
    height: 192px;
    position: relative;
    border-radius: 100%;
    border: 6px solid #F8F8F8;
    box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.1);

    > div {
        width: 100%;
        height: 100%;
        border-radius: 100%;
        background-size: cover;
        background-repeat: no-repeat;
        background-position: center;
    }
}
}

JS.......

function readURL(input) {
 if (input.files && input.files[0]) {
    var reader = new FileReader();
    reader.onload = function(e) {
        $('#imagePreview').css('background-image',      'url('+e.target.result +')');
        $('#imagePreview').hide();
        $('#imagePreview').fadeIn(650);
    }
    reader.readAsDataURL(input.files[0]);
 }
}

$("#imageUpload").change(function() {
    readURL(this);
});

如何在 Angular 6 中使用它?

是我得到这个的链接,请有人帮助我。

标签: angulartypescript

解决方案


对于打字稿,它是这样的。

<input type="file" id="upload-photo" style="display: none" (change)="onSelectFile($event)" />

    onSelectFile(event: any) {
        if (event.target.files && event.target.files[0]) {
            const reader = new FileReader()

            reader.onload = (ev: any) => {
                this.person.photo = ev.target.result
            }
            reader.readAsDataURL(event.target.files[0])

        }
    }

并且仅在 HTML 中显示,您为此编写了两种方式的数据绑定。

或者你可以在这里看到这个链接

https://stackblitz.com/edit/angular-twk2pn?file=src%2Fapp%2Fapp.component.ts


推荐阅读