代码之家  ›  专栏  ›  技术社区  ›  Sachith Wickramaarachchi

如何使用将Base64图像上载到firebase。网芯

  •  0
  • Sachith Wickramaarachchi  · 技术社区  · 4 年前

    在我的应用程序中,图像是Base64字符串,我需要将其存储在Firebase存储中。为了实现这一点,我首先将Base64解码到图像中,然后将其存储到本地服务器中。然后从服务器路径上传到FirebaseStorage。上传到Firebase后,从本地服务器删除图像。我的示例代码如下:,

    string filename = "stackoverflow.jpg";
    var folderPath = Path.Combine(_hostingEnv.WebRootPath, "dev");
    
    //Creating dev foder if not exists
    if (!Directory.Exists(folderPath)) {
      Directory.CreateDirectory(folderPath);
    }
    
    var path = Path.Combine(folderPath, filename);
    File.WriteAllBytes(path, Convert.FromBase64String(req.Data));
    var firebaseAutProvider = new FirebaseAuthProvider(new FirebaseConfig(_configuration["FirebaseConfig:ApiKey"]));
    var firebaseAuthLink = await firebaseAutProvider.SignInWithEmailAndPasswordAsync(_configuration["FirebaseConfig:AuthEmail"], _configuration["FirebaseConfig:AuthPassword"]);
    
    //  CancellationTokenSource can be used to cancel the upload midway
    var cancellation = new CancellationTokenSource();
    
    using(FileStream fileStream = new FileStream(path, FileMode.Open)) {
      var task = new FirebaseStorage(
          _configuration["FirebaseConfig:Bucket"],
          new FirebaseStorageOptions {
            AuthTokenAsyncFactory = () => Task.FromResult(firebaseAuthLink.FirebaseToken),
              ThrowOnCancel = true // when cancel the upload, exception is thrown. By default no exception is thrown
          })
        .Child("dev") //uploading to the firebase's storage dev folder
        .Child(_configuration["FirebaseConfig:ImageFolder"])
        .PutAsync(fileStream, cancellation.Token);
      //task.Progress.ProgressChanged += (s, e) => Console.WriteLine($"Progress: {e.Percentage} %");
      imageAccessLink = await task;
      fileStream.Dispose();
    }
    
    //Delete uploaded file from the local server, after uploading to the firebase
    if (File.Exists(path))
      File.Delete(path);
    

    它工作正常,但我担心的是,我需要在不使用本地服务器的情况下执行此操作,这意味着,我需要直接将Base64上传到firebase,而不将其保存到本地服务器。我该怎么做?我找了找 Upload a base64 image with Firebase Storage .但问题是通过网络来实现这一点。网提前谢谢。

    0 回复  |  直到 4 年前
        1
  •  2
  •   J.Salas    4 年前

    您只需要使用Base64String来创建要发送到Firebase的流(通常它只需要是一个流,而不是一个文件流),比如:

    byte[] bytes = Convert.FromBase64String(imageInbase64);
    
    using(MemoryStream fileStream = new MemoryStream(bytes)) {
      var task = new FirebaseStorage(
          _configuration["FirebaseConfig:Bucket"], 
          //Continue your code ....
    

    如果它真的需要是一个文件流,请在发送之前在内部复制该流

     tmpStream.WriteTo(fileStream);
    

     tmpStream.Position = 0;
     tmpStream.CopyTo(fileStream);
    
        2
  •  1
  •   markovic-m    4 年前

    您可以从同一字节数组创建MemoryStream,而不是将字节数组写入路径并创建要写入firebase的FileStream( Convert.FromBase64String(req.Data) )就像这样:

    MemoryStream stream = new MemoryStream(Convert.FromBase64String(req.Data));
    

    然后将该流而不是文件流传递给PutAsync

    .PutAsync(stream, cancellation.Token);