使用ajax将字符串上传到imgur

vaj7vani  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(185)

我使用此代码将图像匿名上传到imgur。它工作得很好。

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script>
            $("#image_selector").change(function() {
                var reader = new FileReader();
                reader.onload = function(e) {
                    var data = e.target.result.substr(e.target.result.indexOf(",") + 1, e.target.result.length);
                    $("#image_preview").attr("src", e.target.result);
                    $.ajax({
                        url: 'https://api.imgur.com/3/image',
                        headers: {
                            'Authorization': 'Client-ID xxxxxxxxxxxx'
                        },
                        type: 'POST',
                        data: {
                            'image': data,
                            'type': 'base64'
                        },
                        success: function(response) {
                            var x=response.data.link;alert(x);
                            $("#image_preview_2").attr("src", response.data.link);
                        }, error: function() {
                            alert("Error while uploading...");
                        }
                    });
                };
                reader.readAsDataURL(this.files[0]);
            });
        </script>

我尝试编写一个名为upload_to_imgur()的函数,将图像数据uri上载到imgur.com

var img='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHkAAAAhCAYAAAD9JwTTAAAAgElEQVRoge3SQQqAMBAEwf3/p+NZvAkSTFfD3MNWZun4ZvcD9H2QA0EOBDkQ5ECQA0EOBDkQ5ECQA0EOBDkQ5ECQAz2QZ+a41btdYDeGffNxIf94kAN7hQz6X3uNfGK7MXYCrxVBrgc5EORAkANBDgQ5EORAkANBDgQ5EORAkANdAOXnHlhV5RUAAAAASUVORK5CYII=','hello.png';
function upload_to_imgur(img)
{
    var msg="Error";
    .....
    .....
    // if image is uploaded successfully msg= image url I get from imgur.com
    return msg;

}

但是,我编写的函数都不起作用。
对于如何在upload\u to\u imgur()函数中使用第一段代码,您有什么建议吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题