通过 ajax 发送图像的最佳方式

Best way to send image through ajax

我正在编写前端 java 脚本应用程序,我需要在屏幕上发送用户已上传到 java 后端的图像。

最有效的发送方式是什么?我应该获取图像 base64 还是应该将像素存储在矩阵中并发送它们

编辑:我不是在寻找代码解决方案,只是在寻找有关采用方法的建议

我正在使用 base64。在应用程序中缓存更容易。

var image64 = null;
var xhr = $.ajax({
            type: "post",
            url: '/image_url/',
            data: {},
            success: function (o) {
                if (o) {
                    if (!o.err) {

                        image64 = o;
                        $('img').attr('src', 'data:image/png;base64,' + image64);
                    } else {
                        if (o.err && o.err.code === 'session_destroy') {
                            sessionDestroyed()
                        } else if (o.err){
                            alertError(o.err);
                        }
                    }
                }
            },
            error: function (xhr) {
                if (xhr.responseText != '') {
                    alert(xhr.responseText);
                }
            }
        });