简体   繁体   English

在Angular4中渲染缩略图

[英]Render thumbnail in Angular4

I am trying to render an image which I got from a Java service as InputStream , re-send it through NodeJS Express server and finally render it in Angular4 我试图将我从Java服务获得的图像渲染为InputStream ,通过NodeJS Express服务器重新发送它,最后在Angular4中渲染它

Here's what I do: 这是我的工作:

Java Jersey service: Java Jersey服务:

@GET
@Path("thumbnail")
@ApiOperation(
        value = "Gets document preview",
        notes = "Gets document preview"
)
@ApiResponses(value = {
        @ApiResponse(code = 200, message = "Preview of the document")
})
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("image/png")
public Response getDocThumbnail(
        @ApiParam(value = "Entity UUID", required = true) @FormDataParam("uuid") String uuid
) throws RepositoryException, UnknowException, WebserviceException, PathNotFoundException, DatabaseException, AutomationException, AccessDeniedException, ConversionException, IOException {
    RawDocument rawDocument = docCtrl.getDocThumbnail(uuid);
    return Response
            .ok(rawDocument.getInputStream(), "image/png")
            .header("Content-Disposition", "attachment; filename=\" " + rawDocument.getName() + "\"")
            .build();
}

the controller looks like: 控制器看起来像:

public RawDocument getDocThumbnail(String uuid) throws IOException, AccessDeniedException, PathNotFoundException, WebserviceException, RepositoryException, DatabaseException, ConversionException, AutomationException, UnknowException {
    return new RawDocument(
            okmWebSrv.getOkmService().getThumbnail(uuid, ThumbnailType.THUMBNAIL_LIGHTBOX),
            "whatever"
    );
}

Basically it's call to OpenKM SDK to retreive document's thumbnail 基本上是调用OpenKM SDK来检索文档的缩略图

This Java endpoint is called from NodeJS Express 4.15 that is pre-processing some requests for this Java backend. 从NodeJS Express 4.15调用此Java端点,该节点正在预处理对此Java后端的一些请求。 Here's what I do: 这是我的工作:

...compose request options...    
const vedica_res = await rp(options);

let buffered = new Buffer(vedica_res, 'binary');
res.writeHead(200, {
    'Content-Type': 'image/png',
    'Content-disposition': 'attachment;filename=' + 'thumb.png',
    'Content-Length': buffered.length
});

return res.end(buffered, 'binary');

Finally with Angular4 being the initiator of this roundtrip I am trying to render the image like so: 最后,使用Angular4作为此往返的发起者,我试图像这样渲染图像:

this.rest.send('http://localhost:4200/vedica/api/document/thumbnail', RequestMethod.Get,
        {uuid: '19516ea1-657e-4b21-8564-0cb87f29b064'}, true).subscribe(img => {
        // this.preview = img
        var urlCreator = window.URL;
        var url = urlCreator.createObjectURL(img);
        this.thumb.nativeElement.src = url;
    })

The 'img' received is a Blob {size: 81515, type: "image/png"} . 收到的'img'是Blob {size: 81515, type: "image/png"} Console shows no errors but renders no image in the <img #thumb/> tag. 控制台未显示任何错误,但在<img #thumb/>标记中未呈现任何图像。 But I can see that it sets the src=blob:http%3A//localhost%3A3000/4cf847d5-5af3-4c5a-acbc-0201e60efdb7 for it. 但是我可以看到它为它设置了src=blob:http%3A//localhost%3A3000/4cf847d5-5af3-4c5a-acbc-0201e60efdb7 Image just has a broken image icon. 图片只是有一个损坏的图片图标。 When I try to read a cached response in a new tab, its accessible but renders nothing again. 当我尝试在新选项卡中读取缓存的响应时,它可以访问,但是什么也没有呈现。

Can you point out what I'm doing wrong? 您能指出我做错了什么吗? Have tried a lot, but no luck. 尝试了很多,但没有运气。

I think the problem is not the stream is closed early, the problem I think will be in the way is downloaded, take a look here: https://docs.openkm.com/kcenter/view/sdk4j-1.1/document-samples.html#getContent 我认为问题不是流很早关闭,我认为问题将以下载的方式出现,请在此处查看: https : //docs.openkm.com/kcenter/view/sdk4j-1.1/document-samples的.html#的getContent

From the server side ( inde middle between OpenKM and your user interface ) the problem usualy is: 从服务器端(在OpenKM和用户界面之间的中间位置),问题通常是:

//response.setContentLength(is.available()); // Cause a bug, because at this point InputStream still has not its real size.

And you should use 你应该使用

response.setContentLength(new Long(doc.getActualVersion().getSize()).intValue());

resolved this by replacing request-promise with bare request package for making this request to the java BE and piping reply right into the wrapping response of the angular FE: 通过用裸露的request包替换request-promise解决此问题,该request包向Java BE发出此请求,并将响应直接传递到有角FE的包装响应中:

let reply = request(options);
reply.pipe(res);

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

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