コード例 #1
0
    private void uploadFile()
    {
        byte[] file_bytes = GetImage();

        var new_metadata = new Firebase.Storage.MetadataChange();

        new_metadata.ContentType = "image/jpg";

        // Upload the file to the path "myPic.jpg" by PutBytesAsync() method
        storage_ref.Child("myPic.jpg").PutBytesAsync(file_bytes, new_metadata).ContinueWith((Task <StorageMetadata> task) => {
            if (task.IsFaulted || task.IsCanceled)
            {
                // print error messages
                Debug.Log("uploading file doesn't complete: " + task.Exception.ToString());
            }
            else
            {
                Debug.Log("uploading file Done");

                // Fetch the download URL
                storage_ref.Child("myPic.jpg").GetDownloadUrlAsync().ContinueWith((Task <Uri> task2) => {
                    if (!task2.IsFaulted && !task2.IsCanceled)
                    {
                        Debug.Log("Download URL: " + task2.Result);
                    }
                    else
                    {
                        Debug.Log("file not found!!");
                    }
                });
            }
        });

        // Upload the file to the path "myPic.jpg" by PutFileAsync() method

        /*storage_ref.Child("myPic.jpg").PutFileAsync("FilePath/FileName.xxx").ContinueWith((Task<StorageMetadata> task) => {
         *  if (task.IsFaulted || task.IsCanceled)
         *  {
         *      // print error messages
         *      Debug.Log("uploading file doesn't complete: " + task.Exception.ToString());
         *  }
         *  else
         *  {
         *      Debug.Log("uploading file Done");
         *  }
         * });*/
    }
コード例 #2
0
    // Upload file text to Cloud Storage using a byte array.
    public void UploadBytes(string path, byte[] data, UnityEngine.Events.UnityAction <bool, string> callback)
    {
        var storageReference = GetStorageReference(path);
        // Create file metadata including the content type
        var new_metadata = new Firebase.Storage.MetadataChange();

        new_metadata.ContentType = "image/jpeg";
        Debug.Log(String.Format("Uploading to {0} ...", storageReference.Path));
        storageReference.PutBytesAsync(data, new_metadata, null, CancellationToken.None, null)
        .ContinueWith((Task <StorageMetadata> task) =>
        {
            if (task.IsFaulted || task.IsCanceled)
            {
                Debug.Log(task.Exception.ToString());
                // Uh-oh, an error occurred!
                callback(false, "");
            }
            else
            {
                storageReference.GetDownloadUrlAsync().ContinueWith((Task <Uri> taskUri) =>
                {
                    //// Metadata contains file metadata such as size, content-type, and download URL.
                    //Firebase.Storage.StorageMetadata metadata = task.Result;
                    //string download_url = metadata.DownloadUrl.ToString();
                    //Debug.Log("Finished uploading...");
                    //callback(true, download_url);
                    if (task.IsFaulted || task.IsCanceled)
                    {
                        callback(false, "");
                    }
                    else
                    {
                        Uri uri = taskUri.Result;
                        callback(true, uri.ToString());
                    }
                });
            }
        });
    }