图片上传无效(在模式下使用cropper.js进行交易)

时间:2018-10-25 21:07:40

标签: javascript html css cropperjs

我的signup.html中有一个模态,应在上传文件时显示cropper.js中的图像裁剪器。但是,如果我尝试更改要上传的文件,则模式会显示我使用的第一个文件。我在做什么错了?

附言:我是第一次使用cropper.js。

这是我的代码:

signup.html:

{% extends 'base.html' %}

{% load static %}

{% block content %}
<header>
    <img src="{% static 'img/logo-min.png' %}" alt="Logo-min">
</header>
<div class="card-signup">
    <form enctype="multipart/form-data" method="POST" class="post-form">
        <h1>+ Cadastro de novo aluno</h1>
        {% csrf_token %}
        {{ credentials_form }}
        {{ personal_info_form }}
        <button type="submit" class="save btn btn-default">Confirmar</button>
    </form>
</div>
<div id="modal">
    <div class="canvas">
        <a class="close">&times;</a>
        <img class="modal-content" id="image">
        <button class="crop">Cortar</button>
    </div>
</div>
{% endblock %}

image_cropper.js:

showModal = () => {
    let modal = document.querySelector('#modal');
    let image = document.querySelector('#image');

    let file = document.querySelector('input[type=file]').files[0];
    let fileReader = new FileReader();

    fileReader.onload = () => {
        modal.style.display = 'flex';
        image.src = fileReader.result;
        let cropper = new Cropper(image, {
            dragMode: 'move',
            aspectRatio: 1 / 1,
            autoCropArea: 0.65,
            restore: false,
            guides: false,
            center: false,
            highlight: false,
            cropBoxMovable: false,
            cropBoxResizable: false,
            toggleDragModeOnDblclick: false,
        });

        let crop_button = document.querySelector('.crop');
        crop_button.onclick = cropper.crop;
    }

    if (file) {
        fileReader.readAsDataURL(file);
    } else {
        image.src = '';
    }
}

closeModal = () => {
   let modal = document.querySelector('#modal');
   picture_input.value = '';
   modal.style.display = 'none';
}

let picture_input = document.querySelector('#id_picture');
picture_input.onchange = showModal;

let close_button = document.querySelector('.close');
close_button.onclick = closeModal;

文件输入由Django呈现,但是看起来像这样:

<input name="picture" accept="image/*" required="" id="id_picture" type="file">

1 个答案:

答案 0 :(得分:0)

要删除图像实例,您需要使用可用的a方法。在我的代码中,我这样写:

destroy()