在 Angular4 中渲染缩略图
Render thumbnail in Angular4
我正在尝试将从 Java 服务获得的图像渲染为 InputStream
,通过 NodeJS Express 服务器重新发送它并最终在 Angular4 中渲染它
这是我的做法:
Java球衣服务:
@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();
}
控制器看起来像:
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"
);
}
基本上就是调用OpenKM SDK to retreive document's thumbnail
这个 Java 端点是从 NodeJS Express 4.15 调用的,它正在预处理这个 Java 后端的一些请求。
这是我的做法:
...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');
最后,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;
})
收到的'img'是一个Blob {size: 81515, type: "image/png"}
。控制台未显示任何错误,但在 <img #thumb/>
标记中未呈现任何图像。但我可以看到它为它设置了 src=blob:http%3A//localhost%3A3000/4cf847d5-5af3-4c5a-acbc-0201e60efdb7
。图像只有一个损坏的图像图标。
当我尝试在新选项卡中读取缓存的响应时,它可以访问但不再呈现任何内容。
你能指出我做错了什么吗?尝试了很多,但没有成功。
我认为问题不是流被提前关闭,我认为问题是在下载途中,请看这里:
https://docs.openkm.com/kcenter/view/sdk4j-1.1/document-samples.html#getContent
从服务器端(在 OpenKM 和您的用户界面之间),问题通常是:
//response.setContentLength(is.available()); // Cause a bug, because at this point InputStream still has not its real size.
你应该使用
response.setContentLength(new Long(doc.getActualVersion().getSize()).intValue());
通过将 request-promise
替换为裸 request
包来解决此问题,以便向 java BE 发出此请求并将回复直接发送到 angular FE 的包装响应中:
let reply = request(options);
reply.pipe(res);
我正在尝试将从 Java 服务获得的图像渲染为 InputStream
,通过 NodeJS Express 服务器重新发送它并最终在 Angular4 中渲染它
这是我的做法:
Java球衣服务:
@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();
}
控制器看起来像:
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"
);
}
基本上就是调用OpenKM SDK to retreive document's thumbnail
这个 Java 端点是从 NodeJS Express 4.15 调用的,它正在预处理这个 Java 后端的一些请求。 这是我的做法:
...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');
最后,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;
})
收到的'img'是一个Blob {size: 81515, type: "image/png"}
。控制台未显示任何错误,但在 <img #thumb/>
标记中未呈现任何图像。但我可以看到它为它设置了 src=blob:http%3A//localhost%3A3000/4cf847d5-5af3-4c5a-acbc-0201e60efdb7
。图像只有一个损坏的图像图标。
当我尝试在新选项卡中读取缓存的响应时,它可以访问但不再呈现任何内容。
你能指出我做错了什么吗?尝试了很多,但没有成功。
我认为问题不是流被提前关闭,我认为问题是在下载途中,请看这里: https://docs.openkm.com/kcenter/view/sdk4j-1.1/document-samples.html#getContent
从服务器端(在 OpenKM 和您的用户界面之间),问题通常是:
//response.setContentLength(is.available()); // Cause a bug, because at this point InputStream still has not its real size.
你应该使用
response.setContentLength(new Long(doc.getActualVersion().getSize()).intValue());
通过将 request-promise
替换为裸 request
包来解决此问题,以便向 java BE 发出此请求并将回复直接发送到 angular FE 的包装响应中:
let reply = request(options);
reply.pipe(res);