如何将图像从字节数组显示到图像标记

时间:2015-08-08 05:31:52

标签: javascript java jquery ajax web-services

我有一个存储在MYSQL中的图像作为BLOB。现在我已将其检索为字节数组,但我无法弄清楚如何在图像标签中显示它。我搜索了可用的答案,但到目前为止它们都没有帮助我。当我从邮递员那里拨打我的服务时,图像会显示为红色。以下是代码

服务层

 @GET
 @Path("/downloadLogo/{merchantId}")
 @Produces({"image/png", "image/jpeg"})
 public Response downloadLogo(@PathParam("merchantId") Long merchantId) {
  MerchantImage merchantImage=this.merchantService.findMerchantImage(merchantId);
     ResponseBuilder responseBuilder = Response.ok(merchantImage.getMerchantLogo()); //getMerchantLogo() gets the byte array
     responseBuilder.header("Content-Disposition", "attachment; filename=\""+merchantImage.getLogoName()+"\"");
     return responseBuilder.build();
 }

Ajax请求

function downloadData(data,serviceUrl, element){
  if(!authorizedimage()){return false;};
     $.support.cors = true;
   $.ajax({
             type: "GET",
             url: serviceUrl,
             crossDomain: true,
    dataType: "binary",
    headers:{'Content-Type':'image/png','X-Requested-With':'XMLHttpRequest'},
             processData: false,
             success: function (data, status, jqXHR) {alert("success");
    if (typeof fnSuccess !== 'undefined' && $.isFunction(fnSuccess)) {
     fnSuccess('image', data, element);
    }
             },
             error: function (jqXHR, status) {

       $('#image_view').append('<img id="uploaded_image" src="data:image/png;base64,'+[jqXHR.responseText]+'">');   
                console.log(jqXHR, typeof jqXHR.responseText);
    if(jqXHR.statusText == "Unauthorized"){
     logout();
    }
             }
          });
    }

0 个答案:

没有答案