javascript - 通过javascript、ajax发送到服务器的base64代码

标签 javascript java jquery ajax

我正在使用 Html5、Java 脚本、ajax 和 java。我正在将图像从桌面上传到裁剪,裁剪后它显示在同一页面的引导模式中。但我没有得到这个图像的 URL,我得到了一些 Base64 代码,当我发送这个 Base64 代码时它不起作用。

I seen this post but i did not get any solution from this link: https://stackoverflow.com/

此代码为静态图像,第一次显示。

我的代码:

HTML:

  <div class="img-container">
         <img src="../assets/img/picture.jpg" alt="Picture">
    </div>
<div class="modal fade docs-cropped" id="getCroppedCanvasModal" aria-hidden="true" aria-labelledby="getCroppedCanvasTitle" role="dialog" tabindex="-1">
 <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <a class="btn btn-primary" id="download"  download="cropped.png" href="javascript:void(0);">Upload</a>

 </div>
</div>

Java 脚本代码:

     (function () {
            var $image = $('.img-container > img');
            var $download = $('#download');
        $('#getCroppedCanvasModal').modal().find('.modal-body').html(result);
                    if (!$download.hasClass('disabled')) {
                        $download.attr('href', result.toDataURL());
                        //console.log("*****************"+result.toDataURL());
                        var swapUrl = result.toDataURL();
                        console.log("*******" + swapUrl);

//                        document.getElementById('replaceMe').src = swapUrl;

                        $('#download').click(function () {
                            var b = result.toDataURL();
                            $.ajax({
                                url: "/sf/p/customizeText",
                                type: 'GET',
                                data: b,
                                success: function (response) {
                                    console.log("999999999999999999999999999999999----------------" + b)
                                },
                                complete: function (response) {

                                },
                                error: function (response) {

                                }
                            });
                        });
                    }
}

我将 result.toDataURL() 分配给变量 b。但它显示了一些 base64 代码。 我如何将此图像发送到服务器。

我给出一个片段。 enter image description here 请给我一些实现此解决方案的想法。

最佳答案

您好,您也可以检查此解决方案

Javascript代码

    var base64before = document.querySelector('img').src;
    var base64 = base64before.replace(/^data:image\/(png|jpg);base64,/, "");
    var httpPost = new XMLHttpRequest();
    var path = "your url";
    var data = JSON.stringify(base64);

    httpPost.open("POST", path, false);
    // Set the content type of the request to json since that's what's being sent
    httpPost.setRequestHeader('Content-Type', 'application/json');
    httpPost.send(data);

这是我的 Java 代码。

    public void saveImage(InputStream imageStream){
    InputStream inStream = imageStream;

    try {
        String dataString = convertStreamToString(inStream);

        byte[] imageBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(dataString);
        BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageBytes));
        // write the image to a file
        File outputfile = new File("/Users/paul/Desktop/testkey/myImage.png");
        ImageIO.write(image, "png", outputfile);

        }catch(Exception e) {
            System.out.println(e.getStackTrace());
        }
    }



  static String convertStreamToString(java.io.InputStream is) {
    java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
    return s.hasNext() ? s.next() : "";
  }

关于javascript - 通过javascript、ajax发送到服务器的base64代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33251619/

相关文章:

jquery - 在可拖动的 DIV 中添加图像按钮并在单击时切换图像源

javascript - 带有选项卡的 Twitter Bootstrap Modal 默认为第一个选项卡

javascript - jqunit,测试正则表达式有时会失败,为什么?

java - 我知道发生之前并不意味着发生在之前,代码 "A = B + 1; B = 1;"可以产生结果 "A == 2 and B == 1"吗?

javascript - 在 Javascript 中保存切换元素和本地存储

java - 在 onCreateView 中膨胀类时出错...

java - keygen标签的私钥

jquery - Rails jquery tokeninput 返回 "not a function"

javascript - 如何在 PostgreSQL 的 react 中显示图像

javascript - 异步加载 Div 的内容