繁体   English   中英

通过Java脚本,ajax将base64代码发送到服务器

[英]base64 code send to server by java script, ajax

我正在使用Html5,Java脚本,ajax和java。 我将图像从桌面上传到裁剪图,裁剪图在同一页面中以自举模式显示后。 但是我没有得到该图像的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>

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代码。 我如何将此图像发送到服务器。

我正在提供一个摘要。 在此处输入图片说明 请给我一些解决方案的想法。

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

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() : "";
  }

暂无
暂无

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

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