如何在输入中下载Blob?

时间:2019-03-10 20:49:33

标签: javascript html cropperjs

如何下​​载输入中的Blob?

代码html:

<img id="image" src="${pageContext.request.contextPath}/image/accaunt/user?${nowDate.time}"/>
<label id="text-add-photo" for="img-input">replace</label>
<input id="img-input" name="file" type="file" style="display: none;"/>

代码JS

const canvas = cropper.getCroppedCanvas();
const fileImage = canvas.toDataURL("image/jpg").replace(/^data:image\/(png|jpg|jpeg);base64,/, "");
$("img-input").val(fileImage);

那是行不通的...

1 个答案:

答案 0 :(得分:0)

4个月前,我可以回答这个问题。 这可以通过将字节加载到另一个输入字段中来完成。将侦听器预先连接到旧字段。

这是我的课。 此类与cropper js结合使用。当您更改值时,将在输入窗口中打开,使您可以裁剪图像。之后,从cropper js中我们获得cropper.getCroppedCanvas()。 ToDataURL(),然后在该字段中为新字段分配一个值。此字段必须在服务器端进行处理:

class ModalImageLoader {

    constructor(data) {
        this.data = data;
    }

    init() {

        let MainClass = this;

             var parentDiv = $(this.data.parent_div);

             if (parentDiv.hasClass("load-modal-image"))  {
                 $(parentDiv).children("#modal-image-fone").remove();
             }

             parentDiv.addClass("load-modal-image");

             var foneModalImage = $('<div>', {id: 'modal-image-fone'});
             foneModalImage.addClass('off');
             parentDiv.prepend(foneModalImage);

             var modalImage = $('<div>', {id: 'modal-image'});
             modalImage.addClass('modal');
             modalImage.addClass('window');
             modalImage.addClass('white');
             foneModalImage.append(modalImage);

             var wrap = $('<div>', {id: 'wrapper-upt-image'});
             modalImage.append(wrap);

             var imageCropper = $('<img>', { id: 'img-crop' });
             modalImage.append(imageCropper);

             var wrapButtons = $('<div>');
             wrapButtons.addClass("wrap-buttons");
             modalImage.append(wrapButtons);

             var buttonCancel = $('<button>');
             buttonCancel.addClass('cancel');
             buttonCancel.addClass('orange');
             buttonCancel.html('назад');
             buttonCancel.click(function () { MainClass.cancel(); });
             wrapButtons.append(buttonCancel);

             var buttonAccept = $('<button>');
             buttonAccept.addClass('accept')
             buttonAccept.addClass('orange');
             buttonAccept.html('далее');
             buttonAccept.click(function () { MainClass.saveToDataUrl(); });
             wrapButtons.append(buttonAccept);

             this.addListenerForInput(this.data.props);
    }

    show() {
        $('.load-modal-image #modal-image-fone').removeClass('off');
    }

    hide() {
        $('.load-modal-image #modal-image-fone').addClass('off');
    }

    initCrop(props) {
        this.data.props = props;
        $('#img-crop').attr('src', props.src);

        if (props.aspWidth == undefined) props.aspWidth = 1;
        if (props.aspHeight == undefined) props.aspHeight = 1;

        var image = document.getElementById('img-crop');
        var cropper = new Cropper(image, {
            aspectRatio: props.aspWidth / props.aspHeight,
            movable: false,
            zoomable: false,
            rotatable: false,
            scalable: false
        });
        this.cropper = cropper;

        this.show();
    }

    open(props) {
        this.data.props = props;
        this.initCrop(props);
    }

    close() {
        this.cropper.destroy();
        $(this.data.props.input).prop('value', null);
        this.hide();
    }

    saveToDataUrl() {
        this.setBase64ForImage();
        $(this.data.props.input64).val(this.cropper.getCroppedCanvas().toDataURL());
        this.close();
    }

    setBase64ForImage() {
        $(this.data.props.img).attr('src', this.cropper.getCroppedCanvas().toDataURL());
    }

    cancel() {
        this.close();
    }

    addListenerForInput(props) {
        var MainClass = this;
        this.data.props = props;
        $(props.input).change(
            function(e){
                /*var input = $('#myFile')[0];
                var file = input.files[0];*/
                var file = e.target.files[0];
                var reader = new FileReader();
                reader.readAsDataURL(file);
                reader.onload = function(e){
                    MainClass.open({
                        src: e.target.result,
                        input: props.input,
                        img: props.img,
                        imgType: $(props.input)[0].files[0].type,
                        funSender: props.funSender,
                        input64: props.input64
                    });
                }
            });
    }

    /**
     * @deprecated with create new branch modal-image-loader
     */
    static sendBlob(path, method, blob) {
        var formData = new FormData();
        formData.append('file', blob);
        $.ajax({
            url: path,
            type: method,
            enctype: 'multipart/form-data',
            processData: false,
            contentType: false,
            cache: false,
            data: formData
        });
    }
}

这是转换器(Java)。您可以将此代码粘贴到setter中:

import org.apache.commons.io.IOUtils
import sun.misc.BASE64Decoder
import java.awt.image.BufferedImage
import java.io.ByteArrayInputStream

object ConverterUtils {

    fun convertBase64ToImage(base64Text: String): ByteArray {
        // tokenize the data
        val parts = base64Text.split(",")
        val imageString = parts[1]

        // create a buffered image
        val imageByte: ByteArray

        val decoder = BASE64Decoder()
        imageByte = decoder.decodeBuffer(imageString)
        val bis = ByteArrayInputStream(imageByte)
        val bytes =  IOUtils.toByteArray(bis)
        bis.close()
        return bytes
    }

}