示例#1
0
 private void Operations_OnUploadCompleted(object sender, BlobCompleteEventArgs e)
 {
     OnUploadCompleted?.Invoke(this, e);
 }
示例#2
0
        async void UploadProgress(UploadOperation upload)
        {
            LogStatus(string.Format("Progress: {0}, Status: {1}", upload.Guid, upload.Progress.Status));
            MainPage.Current?.ShowMediaUploadingUc();

            BackgroundUploadProgress progress = upload.Progress;

            double percentSent = 100;

            if (progress.TotalBytesToSend > 0)
            {
                percentSent = progress.BytesSent * 100 / progress.TotalBytesToSend;
            }
            SendNotify(percentSent);
            LogStatus(string.Format(" - Sent bytes: {0} of {1} ({2}%), Received bytes: {3} of {4}",
                                    progress.BytesSent, progress.TotalBytesToSend, percentSent,
                                    progress.BytesReceived, progress.TotalBytesToReceive));

            if (progress.HasRestarted)
            {
                LogStatus(" - Upload restarted");
            }

            if (progress.HasResponseChanged)
            {
                var resp = upload.GetResponseInformation();
                LogStatus(" - Response updated; Header count: " + resp.Headers.Count);
                var          response = upload.GetResultStreamAt(0);
                StreamReader stream   = new StreamReader(response.AsStreamForRead());
                Debug.WriteLine("----------------------------------------Response from upload----------------------------------");

                var st = stream.ReadToEnd();
                Debug.WriteLine(st);
                if (UploadItem.IsVideo)
                {
                    if (!UploadItem.IsStory)
                    {
                        var thumbStream = (await UploadItem.ImageToUpload.OpenAsync(FileAccessMode.Read)).AsStream();
                        var bytes       = await thumbStream.ToByteArray();

                        var img = new InstaImageUpload
                        {
                            ImageBytes = bytes,
                            Uri        = UploadItem.ImageToUpload.Path
                        };
                        await UploadSinglePhoto(img, UploadId);
                    }
                    await Task.Delay(15000);

                    if (!UploadItem.IsStory)
                    {
                        await FinishVideoAsync();
                    }
                    await Task.Delay(1500);
                }
                if (!UploadItem.IsAlbum)
                {
                    await ConfigurMediaAsync();
                }
                else
                {
                    OnUploadCompleted?.Invoke(this);
                }
            }
        }
示例#3
0
        public async Task UploadFileAsync(string path, string share, string filename, string contentType = "application/octet-stream", CancellationToken token = default(CancellationToken))
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("path");
            }

            if (string.IsNullOrEmpty(share))
            {
                throw new ArgumentNullException("share");
            }

            if (string.IsNullOrEmpty("filename"))
            {
                throw new ArgumentNullException("filename");
            }

            if (!File.Exists(path))
            {
                throw new FileNotFoundException("path");
            }

            Exception error = null;
            Stopwatch watch = new Stopwatch();

            watch.Start();
            double time             = watch.Elapsed.TotalMilliseconds;
            long   bytesTransferred = 0;

            IProgress <StorageProgress> progressHandler = new Progress <StorageProgress>(
                progress =>
            {
                bytesTransferred = bytesTransferred < progress.BytesTransferred ? progress.BytesTransferred : bytesTransferred;
                FileInfo info    = new FileInfo(path);
                if (watch.Elapsed.TotalMilliseconds > time + 1000.0 && bytesTransferred <= progress.BytesTransferred)
                {
                    OnUploadBytesTransferred?.Invoke(this, new BytesTransferredEventArgs(share, filename, bytesTransferred, info.Length));
                }
            });

            try
            {
                CloudFileShare     choudShare = client.GetShareReference(share);
                CloudFileDirectory dir        = choudShare.GetRootDirectoryReference();
                CloudFile          file       = dir.GetFileReference(filename);
                file.Properties.ContentType = contentType;
                await file.UploadFromFileAsync(path, default(AccessCondition), default(FileRequestOptions), default(OperationContext), progressHandler, token);
            }
            catch (Exception ex)
            {
                if (!(ex.InnerException is TaskCanceledException))
                {
                    error = ex;
                    throw ex;
                }
            }
            finally
            {
                watch.Stop();
                OnUploadCompleted?.Invoke(this, new BlobCompleteEventArgs(share, filename, token.IsCancellationRequested, error));
            }
        }