public Task <string> UploadMoments(FileData fileData) { TaskCompletionSource <string> ResultCompletionSource = new TaskCompletionSource <string>(); string fileExtension = GetMomentExtension(fileData.FileName); string fileNameStr = fileData.FileName; string path = "moments/" + Guid.NewGuid().ToString() + fileExtension; try { StorageReference storageReference = storage.GetReference(path); UploadTask uploadTask = storageReference.PutStream(fileData.GetStream()); uploadTask.AddOnCompleteListener(new OnCompleteEventHandleListener((Android.Gms.Tasks.Task uploadFile) => { if (uploadFile.IsSuccessful) { var TaskResult = uploadFile.Result; var uri = ((UploadTask.TaskSnapshot)TaskResult).DownloadUrl.ToString(); ResultCompletionSource.SetResult(uri); } })); } catch (Exception e) { ResultCompletionSource.TrySetException(e); } return(ResultCompletionSource.Task); }
public Task <Attachment> UploadFile(FileData fileData) { TaskCompletionSource <Attachment> ResultCompletionSource = new TaskCompletionSource <Attachment>(); string fileExtension = GetExtension(fileData.FileName); string fileNameStr = fileData.FileName; string path = "userFile/" + Guid.NewGuid().ToString() + fileExtension; try { StorageReference storageReference = storage.GetReference(path); StorageMetadata storageMetadata = new StorageMetadata.Builder() .SetCustomMetadata("FileName", fileNameStr) .Build(); UploadTask uploadTask = storageReference.PutStream(fileData.GetStream(), storageMetadata); uploadTask.AddOnCompleteListener(new OnCompleteEventHandleListener((Android.Gms.Tasks.Task uploadFile) => { if (uploadFile.IsSuccessful) { var TaskResult = uploadFile.Result; var uri = ((UploadTask.TaskSnapshot)TaskResult).DownloadUrl.ToString(); Attachment attachment = new Attachment(fileNameStr, uri); ResultCompletionSource.SetResult(attachment); } })); } catch (Exception e) { ResultCompletionSource.TrySetException(e); } return(ResultCompletionSource.Task); }
public static Task <string> UploadImage(Stream imageStream, string storagePath) { var tcs = new TaskCompletionSource <string>(); StorageReference storageRef = FirebaseStorage.Instance.GetReference(storagePath); var uploadTask = storageRef.PutStream(imageStream); uploadTask.AddOnCompleteListener(new OnUploadImageCompleteListener(tcs, storageRef)); return(tcs.Task); }
public async System.Threading.Tasks.Task saveImage(Stream imgStream, string nameImage) { try { if (Connectivity.NetworkAccess == NetworkAccess.Internet) { FirebaseStorage storage = FirebaseStorage.Instance; StorageReference storageRef = storage.GetReferenceFromUrl("gs://eplant-947b2.appspot.com/Plantas"); StorageReference imageRef = storageRef.Child(nameImage); imageRef.PutStream(imgStream).AddOnSuccessListener(this).AddOnFailureListener(this); } } catch (Exception ex) { ExceptionHandler.LogAndSendException(this, nameof(saveImage), ex); } }
/// <summary> /// Encapsulate Card into a single file for uploading and sharing to Firebase /// </summary> private void uploadCardMedia(StorageReference sr_cardMedias, Card card) { // Upload media to cloud foreach (AbMediaContent media in card.mediaList) { string mediaFileName = Path.GetFileName(media.filePath); StorageReference sr_curCardMedia = sr_cardMedias.Child(mediaFileName); FileStream fs = new FileStream(media.filePath, FileMode.Open); //StorageMetadata metadata = new StorageMetadata.Builder() // .SetCustomMetadata("MediaContentType", media.mediaType.ToString()).Build(); UploadTask uploadTask = sr_curCardMedia.PutStream(fs); UploadListener uploadListener = new UploadListener(fs); uploadTask.AddOnFailureListener(uploadListener); uploadTask.AddOnSuccessListener(uploadListener); } // TODO add upload failure and success handling? }
public IStorageTransferTask PutStream(Stream stream, IStorageMetadata metadata = null) { return(metadata == null ? _wrapped.PutStream(stream).ToAbstract() : _wrapped.PutStream(stream, metadata.ToNative()).ToAbstract()); }