【问题标题】:how to put image in json as data如何将图像作为数据放入 json
【发布时间】:2017-05-18 07:48:26
【问题描述】:

我正在编写rest api的代码,其中我在页面“imageTest”上,带有图像和按钮的按钮“onclick事件”将调用函数“foo()”

          @RequestMapping(value = "/imageUpload2/", method = RequestMethod.POST, produces = "application/json")
public ResponseEntity profilePicTest(@RequestBody String data) {
    System.out.println("imageString length : " + data.length());
    System.out.println("imageUpload : " + data);

        return new ResponseEntity(null, HttpStatus.OK);

javascript部分

    function foo() {
    alert('uploading image to imageupload2 url');           
    var profpic = document.getElementById("profpic");
    alert('uploading image1122334455')
    jQuery.ajax( {
        url: 'http://localhost:8090/EventApp/imageUpload2/',
        type: 'POST',
        data: { img: profpic },
        success: function( response ) {
            // response
            }
    } ); alert('uploading done')
    }

如果我可以通过 javascript(使用 base64 之类的编码)上传图像,restcontroller 可以接受该图像字符串进一步转换为 byte[] 数组并插入到 oracle db blob 列中,反之亦然以发送它返回

-----问题部分---- 图像未以字符串形式上传到 json 请任何人帮助我...!

【问题讨论】:

    标签: javascript json rest image-uploading oracle12c


    【解决方案1】:

    data 的值应该只是输入中的文件对象。

    HTML

    <input type="file" id="profpic" />
    

    JS

    var file = $("#profpic")[0].files[0];
    
    jQuery.ajax( {
      url: 'http://localhost:8090/EventApp/imageUpload2/',
      type: 'POST',
      data: file,
      success: function( response ) {
        // response
      }
    });
    

    注意:这在许多旧版浏览器中不起作用。 IE10+ 没问题,但任何比这更旧的都需要不同的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-22
      • 2011-11-14
      • 1970-01-01
      • 1970-01-01
      • 2020-02-25
      • 2016-01-26
      • 1970-01-01
      相关资源
      最近更新 更多