将相对映像转换为base64或使用节点将相对映像上传到Firebase存储

时间:2019-02-22 19:26:35

标签: firebase base64 nodes firebase-storage

有人以前做到过吗?我不断收到以下错误。 我还使用base64-img将本地图像转换为base64。 错误:

throw invalidFormat(format, 'Invalid character found');
        ^
FirebaseError: Firebase Storage: String does not match format 'base64': Invalid character found

请求:

    ...

    import base64Img from 'base64-img';
    const app = express()

    app.get('/', function (req, res) {
      res.send('hello world')

      let url = './images/girl.jpg'

      base64Img.base64(url, function(err, data) {
        console.log(data)
        let fileName = "girl.jpg";

        const metadata = {
          contentType: "image/jpeg"
        };

        let strImage = data.split("base64,")[1];
        console.log(strImage)
        let storageRef = storage.ref(`images/${fileName}`);
        storageRef.putString(strImage, 'base64', metadata)
      });

    ....

1 个答案:

答案 0 :(得分:0)

我决定从头开始将图像转换为base64并上传本地图像。这是我要执行的原始任务。经过大量的挖掘,这可以将您的相对图像上传到Firebase存储。

// open a UDP socket
m_sock = socket( AF_INET, SOCK_DGRAM, IPPROTO_IP );
if ( m_sock < 0 )
{
   std::cerr << "ERROR: create socket failed ..." << std::endl;
   return( false );
}

m_saddr.sin_family      = AF_INET;
m_saddr.sin_port        = htons( UDP_PORT ); // listen on port defined UDP_PORT    
m_saddr.sin_addr.s_addr = htonl(INADDR_ANY); // bind socket to any interface

// Disable loopback
char loopch{ 0 };

int rc =  setsockopt( m_sock, IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&loopch, sizeof( loopch ) );

if ( rc < 0 )            
{
   std::cerr << "ERROR: setsockopt( ) to disable loopback failed, rc = " 
             << rc << std::endl;

    return( false );
}

rc = bind( m_sock, (struct sockaddr *)&m_saddr, sizeof( struct sockaddr_in ) );

if ( rc < 0 )
{
   std::cerr << "ERROR: binding socket to interface failed ..." << std::endl;
   exit( -1 );
}

// JOIN multicast group on default interface        
m_imreq.imr_multiaddr.s_addr = inet_addr( UDP_ADDRESS );
m_imreq.imr_interface.s_addr = htonl( INADDR_ANY ); // use DEFAULT interface

rc = setsockopt( m_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&m_imreq, sizeof( struct ip_mreq ) );

if ( rc < 0)
{
   std::cerr << "ERROR: Setting local interface error" << std::endl;
    return false;
}

socklen_t m_socklen = sizeof(struct sockaddr_in);

while ( 1 )
{
    rc = recvfrom( m_sock, buffer, MAX_BUFFER_SIZE, 0, 
            (struct sockaddr *)&m_saddr, &m_socklen );

    if ( rc < 0 )
    {
        std::cerr << "ERROR: receive multicast message failed ..." << std::endl;

         break;
     }
     else
     {
         std::cout << "Received multicast ..." << std::endl;                
     }
}