简体   繁体   English

将PNG图片从后端保存到前端到本地Angular Project文件夹

[英]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.

相关问题 如何将前端 ReactJs 项目文件夹与 NodeJs 后端连接? - How to connect Frontend ReactJs Project Folder with NodeJs Backend? 如何将图片从前端上传到后端 - how to upload image from frontend to backend 使用javascript将画布另存为PNG图像在特定文件夹中 - Save canvas as PNG image in specific folder with javascript 如何保存从画布转换为本地文件夹的图像? - how to save an image which is converted from a canvas in to my local folder? 将Image jpg,png,jpeg…保存在CloudBoost后端的一列中作为服务 - Save Image jpg, png, jpeg… in a column in CloudBoost backend as a service 如何使用 API 在我的后端访问我的 angular 前端图像 - How to access to my angular frontend image in my backend with API 如何使用纯javascript或jQuery将我的HTML5画布屏幕作为png图像保存到我项目的特定文件夹中? - How can I save my HTML5 canvas screen as a png image to a specific folder of my project with only pure javascript or jQuery? 如何在选择选项 angular 9 处将数据从后端绑定到前端 - How to bind data from backend into frontend at select option angular 9 从后端(Java / Jersey / REST)将数据发送到前端(Angular JS) - Send data to Frontend(Angular JS) from backend (Java/Jersey/REST) 如何安全地从后端发送图像以显示在前端 HTML? - How to securely send image from backend to be displayed in frontend HTML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM