base64代码通过javascript,ajax发送到服务器

问题描述 投票:1回答:1

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

我看过这篇文章,但我没有从这个链接得到任何解决方案: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>

Javascript代码:

     (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 java jquery ajax
1个回答
0
投票

嗨,你也可以检查这个解决方案

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() : "";
  }
© www.soinside.com 2019 - 2024. All rights reserved.