注入裁剪的文件作为输入:文件值

Ara*_*810 6 javascript jquery image flatfilereader croppie

我正在使用 jQuery Croppie lib 来裁剪用户上传的文件。当用户上传文件时,我打开用户可以裁剪图像的模式。之后我想将此裁剪图像设置为文件输入值,以便用户可以在一切准备就绪后提交表单,但我不知道如何“设置”裁剪图像作为输入值。

这是我的代码。

$scope.openCropModal = function(files) {
            if (!(files[0] instanceof Object) || (fileUploadMaxSize * 1100000) < files[0].size) {
                Alertify.alert('File size must be less than ' + fileUploadMaxSize + 'mb');

                return false;
            }
            $('#cropLogoModal').modal({});

            var $uploadCrop = $('#cropperMainContainer').croppie({
                viewport: {
                    width: 200,
                    height: 200,
                    type: 'square'
                },
                boundary: {
                    width: 300,
                    height: 300
                },
                enableExif: true,
                enableOrientation: true,
                orientation: 4,
            });

            var reader = new FileReader();

            reader.onload = function (e) {
                $uploadCrop.croppie('bind', {
                    url: e.target.result
                }).then(function() {

                });

            };

            reader.readAsDataURL(files[0]);

            $('#ready').on('click', function() {
                $uploadCrop.croppie('result', 'blob').then(function(blob) {

                });
            });

            $('.vanilla-rotate').on('click', function() {
                $uploadCrop.croppie('rotate', parseInt($(this).data('deg')));
            });
        }
Run Code Online (Sandbox Code Playgroud)

Jus*_*nas 3

不要将其作为文件附件,而是为 bas64 编码的 Blob 数据创建隐藏字段。

        var reader = new FileReader();

        reader.onload = function (e) {
            $uploadCrop.croppie('bind', {
                url: e.target.result
            }).then(function(blob) {
                $('#hiddenContent').val(btoa(blob));
            });
        };
Run Code Online (Sandbox Code Playgroud)

与在服务器端相比,读取此 Base64 编码的图像内容并将其放入新文件中。

警告代码未经测试。

  • 我的 lib 返回 base64 以及 blob。我只是想知道是否可以仅使用我的文件输入来完成此操作。 (2认同)