简体   繁体   English

从客户端向服务器发送图像数据(base64)

[英]Sending image data (base64) from client to server

This is my code for sending and image data over Errai Jax-RS: 这是我通过Errai Jax-RS发送和图像数据的代码:

    Model model = new Model();
    GWT.log("Before setting byte array");
    model.setImage(Base64.base64ToByteArray(base64)); 
    GWT.log("After setting byte array");
    modelService.call(new RemoteCallback<String>() {
        @Override
        public void callback(String hash) {
            GWT.log("Model upload with hash - " + hash);
        }
    }, new ErrorCallback() {
        @Override
        public boolean error(Message message, Throwable throwable) {
            showErrorModal();
            return false;
        }
    }).createModel(model);

The log shows that "After setting byte array" is logged before the client (browser) break, or hang. 该日志显示在客户端(浏览器)中断或挂起之前记录了“设置字节数组之后”。 So the problem seems to be with the service call. 因此问题似乎出在服务呼叫上。 Is it wrong to send base64 data like this? 这样发送base64数据是否错误? The test data send it about 350K bytes in size. 测试数据发送给它约350K字节。

I would send the image in String instead of in a byte array. 我将以String而不是字节数组形式发送图像。

So change the image attribute in your model to String, and convert from base64 string to byte array in server side when you need to handle the image. 因此,在需要处理图像时,将模型中的image属性更改为String,并在服务器端将base64字符串转换为字节数组。

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

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