简体   繁体   English

获取图像角度或节点的 base64 的最佳方法

[英]Best way to get base64 of image angular or node

I want to get the base64 data of the images.我想获取图像的 base64 数据。 I am working in angular 6 as Frontend and NodeJS as backend.我在 angular 6 作为前端工作,NodeJS 作为后端工作。 From where should I get the base64 string?我应该从哪里获得 base64 字符串? Either it is computed on frontend or I have to convert it on the backend and then send it back to the frontend?它是在前端计算的还是我必须在后端转换然后将其发送回前端? And what is the best way to get the base64 data on frontend as well as on the backend在前端和后端获取 base64 数据的最佳方法是什么

You can convert into base64 on both angular and nodejs.您可以在 angular 和 nodejs 上转换为 base64。 Depends on the requirement.取决于要求。 If you have flexibility to convert on either side, I will suggest you to go with nodejs.如果您可以灵活地在任何一方进行转换,我建议您使用 nodejs。 If you try on angular,this conversion will be done on browser, where browser might be opened on low end machine, and some file api are specific to browser.如果您尝试使用 angular,此转换将在浏览器上完成,浏览器可能会在低端机器上打开,并且某些文件 api 是特定于浏览器的。 Filereader or FIleonload will take more time if the file size is too much.如果文件太大,Filereader 或 Fileonload 将花费更多时间。

But, where as most of the server will be hosted in high end machine, it will increase performance as well as it solve the over head of handling browser compatability.但是,由于大多数服务器将托管在高端机器上,因此它将提高性能并解决处理浏览器兼容性的开销。

You can get the base64 by below Code in ClientSide angular您可以通过以下 ClientSide angular 中的代码获取 base64

handleFileSelect(evt){
      var files = evt.target.files;
      var file = files[0];

    if (files && file) {
        var reader = new FileReader();

        reader.onload =this._handleReaderLoaded.bind(this);

        reader.readAsBinaryString(file);
    }
  }



  _handleReaderLoaded(readerEvt) {
     var binaryString = readerEvt.target.result;
            this.base64textString= btoa(binaryString);
            console.log(btoa(binaryString));
    }

You can convert the image on both the front-end and backend.您可以在前端和后端转换图像。 If you don't need to store the image, then it is better to convert it on frontend.如果您不需要存储图像,那么最好在前端转换它。 Otherwise, if you are going to store the image to the base64 format, then you can convert it on server side.否则,如果您要将图像存储为 base64 格式,则可以在服务器端对其进行转换。

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

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