예제 #1
0
        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);
        }
예제 #2
0
        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);
        }
        private Task Upload(UploadTask uploadTask, IProgress <IUploadState>?progress = null, CancellationToken cancellationToken = default, PauseToken pauseToken = default)
        {
            var tcs = new TaskCompletionSource <bool>();

            uploadTask.AddOnCompleteListener(new OnCompleteHandlerListener(task =>
            {
                if (task.IsSuccessful)
                {
                    tcs.SetResult(true);
                }
                else
                {
                    tcs.SetException(ExceptionMapper.Map(task.Exception));
                }
            }));

            if (progress != null)
            {
                uploadTask.AddOnProgressListener(new OnProgressHandlerListener(snapshot =>
                {
                    var uploadTaskSnapshot = snapshot.JavaCast <UploadTask.TaskSnapshot>();
                    progress.Report(new UploadTaskSnapshotWrapper(uploadTaskSnapshot !));
                }));
            }

            if (cancellationToken != default)
            {
                cancellationToken.Register(() => uploadTask.Cancel());
            }

            if (pauseToken != default)
            {
                pauseToken.SetStorageTask(new StorageTaskWrapper(uploadTask));
            }

            return(tcs.Task);
        }