[英]Save PNG image from Backend to Frontend to local Angular Project folder
I want to save a PNG image received from Backend (Java Project) to a folder inside my Angular Project. 我想将从后端(Java项目)收到的PNG图像保存到Angular Project内的文件夹中。 So far I can only save the image under Downloads/ folder of the PC and you can see how the file is downloaded.
到目前为止,我只能将图像保存在PC的Downloads /文件夹下,您可以看到文件的下载方式。 What I want is to silently download the image in my project (when I check the folder to see the new image stored).
我想要的是在项目中静默下载图像(当我检查文件夹以查看存储的新图像时)。
Backend: 后端:
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("getImage")
public Response getImage() {
File dir = new File(Utilities.IMAGE_DIRECTORY);
File[] directoryListing = dir.listFiles();
String encodedImages = null;
// Get the first image stored in Backend project folder
try {
if (directoryListing != null) {
// Encode the image in Base64 and save it in a string
encodedImages = Base64
.getEncoder()
.withoutPadding()
.encodeToString(
Files.readAllBytes(directoryListing[0].toPath()));
}
} catch (IOException e) {
e.printStackTrace();
...
}
// Send the base64 string to Frontend
return Response
.status(Response.Status.OK)
.entity(encodedImages)
.build();
}
Frontend: 前端:
/* Extract Image */
getImage() {
this
.http
.get(this.baseUrl + "getImage", { responseType:
'text' })
.subscribe((res) => {
console.log("I received the image: \n" + res);
// Decode from base64 to PNG
var decodedImage = atob(res);
var blob = new Blob([decodedImage], { type: 'image/png' });
//this method saves the image in Downloads/ and it is not silent
saveAs(blob, 'imageFileName.png');
});
}
Since your user will not need the image it self why to download it? 既然您的用户将不需要它自己的图像,为什么要下载它? You don't need to download the image just for using it.
您不需要下载图像就可以使用它。 Only if your user need to download the image you could go with and "Save File" system.
仅当您的用户需要下载图像时,才可以使用“保存文件”系统。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.