简体   繁体   English

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

[英]Upload of image doesn't work (dealing with cropper.js on a modal)

I have a modal in my signup.html that should display a image cropper, from cropper.js when a file is uploaded. 我的signup.html中有一个模式,应该在上传文件时显示来自cropper.js的图像裁剪器 But if I try to change the file I'm uploading, the modal shows up with the first file that I used. 但是,如果我尝试更改要上传的文件,则模式会显示我使用的第一个文件。 What am I doing wrong? 我究竟做错了什么?

PS: Is the first time I'm using cropper.js. PS:这是我第一次使用cropper.js。

Here is my code: 这是我的代码:

signup.html: 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: 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;

The file input is rendered by Django, but it looks like this: 输入的文件由Django呈现,但是看起来像这样:

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

To delete the instance of the image, you need to use the destroy() method available. 要删除图像实例,您需要使用可用的destroy()方法。 In my code, I put it like this: 在我的代码中,我这样写:

close_button.onclick = closeModal = () => {
    modal.style.display = 'none';
    cropper.destroy();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM