简体   繁体   English

从bootstrap模式获取图像URL到java脚本

[英]Get image URL from bootstrap modal to java script

I am modifying one image and show it in a modal, but I am not getting the image URL because this image modified and show in bootstrap modal. 我正在修改一个图像并以模态显示它,但我没有得到图像URL,因为此图像已修改并显示在bootstrap模式中。 I want to get URL for this image in JavaScript for upload to server. 我想在JavaScript中获取此图像的URL以便上传到服务器。

I have already seen this link but am not satisfied this solution: https://stackoverflow.com/questions/18754900 我已经看过这个链接但不满意这个解决方案: https//stackoverflow.com/questions/18754900

在此输入图像描述

HTML CODE: HTML代码:

 <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
     <a class="btn btn-primary" id="upload" download="cropped.png" href="javascript:void(0);">Upload</a>
 </div>

JavaScript Code: JavaScript代码:

        $('#upload').click(function () {
                        var b = result.toDataURL();
                        $.ajax({
                            url: "/sf/p/customizeText",
                            type: 'GET',
                            data: b,
                            success: function (response) {

                            },
                            complete: function (response) {

                            },
                            error: function (response) {

                            }
                        });
                    });

I want to send this cropped image URL to server, but i am not getting URL from this image because cropped image will be new each time and it is temporary save in browser after reload image is lose. 我想将这个裁剪后的图片网址发送到服务器,但我没有从此图片中获取网址,因为裁剪后的图片每次都是新的,并且在重新加载图片丢失后,它会暂时保存在浏览器中。 I am save this image in variable b, But this is in bas64 form, we can directly send to /sf/p/customizeText (url) by ajax? 我将此图像保存在变量b中,但这是bas64格式,我们可以通过ajax直接发送到/ sf / p / customizeText(url)吗?

Can we assign result.toDataURL() in varaible b and pass in ajax Like 我们可以在变量b中分配result.toDataURL()并传入ajax Like

  data: b, 

Please give me some idea for achieve this solution. 请给我一些实现此解决方案的想法。

The image cropped will most likely be a base64 encoded image. 裁剪的图像很可能是base64编码的图像。 You should post the HTML of the image tag. 您应该发布图像标记的HTML。 You can get the image sorurce via attr() . 你可以通过attr()获得图像sorurce。

var imageSrc = $('#id').attr('src'); //data%3Aimage%2Fpng%3Bbase64%2CiVBORw0KGgoAAAANSUhE

You have to get the src of the image tag via js and send it to the server. 您必须通过js获取图像标记的src并将其发送到服务器。 Then you can either save the string and use it directly as image or decode it. 然后,您可以保存字符串并将其直接用作图像或解码 I give you a little example on how to do that with PHP and Java. 我给你一个关于如何用PHP和Java做这个的例子。

PHP PHP

//save your data into a variable - last part is the base64 encoded image
$encoded = "data%3Aimage%2Fpng%3Bbase64%2CiVBORw0KGgoAAAANSUhE";
//decode the url, because we want to use normal charackters to use explode
$decoded = urldecode($encoded);
//explode at ',' - the last part should be the encoded image now
$exp = explode(',', $decoded);
//we just get the last element with array_pop
$base64 = array_pop($exp);
//decode the image and finally save it
$data = base64_decode($base64);
$file = 'data.png';
//make sure you are the owner and have the rights to write content
file_put_contents($file, $data);

Java Java的

byte[] data = Base64.decodeBase64(crntImage);
try (OutputStream stream = new FileOutputStream("c:/decode/abc.bmp")) {
    stream.write(data);
}

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

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