protected override async Task <UploadResponse> InternalUploadAsync()
        {
            int       tryCount      = 0;
            Exception lastException = null;

            Stream stream = null;

            try
            {
                stream = File.OpenRead();
                while (tryCount < 3)
                {
                    try
                    {
                        var httpClient = GetHttpClient();

                        using (var requestMessage = new HttpRequestMessage(
                                   HttpMethod.Post,
                                   GetChunkUriForStandardUploads()))
                        {
                            using (var multipartFormContent = new MultipartFormDataContent("upload-" + Guid.NewGuid().ToString("N")))
                            {
                                BaseRequestProvider.TryAddCookies(Client, requestMessage);

                                var streamContent = new StreamContentWithProgress(stream, OnProgress);
                                streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                                multipartFormContent.Add(streamContent, "File1", File.Name);
                                requestMessage.Content = multipartFormContent;

                                var responseMessage =
                                    await
                                    httpClient.SendAsync(
                                        requestMessage,
                                        CancellationToken.GetValueOrDefault(System.Threading.CancellationToken.None));

                                MarkProgressComplete();

                                return(await GetUploadResponseAsync(responseMessage));
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        lastException = exception;
                        stream.Seek(0, SeekOrigin.Begin);
                        tryCount++;
                    }
                }
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }


            throw new ApplicationException("Upload failed after 3 tries", lastException);
        }
        public override UploadResponse Upload(Dictionary<string, object> transferMetadata = null)
        {
            SetUploadSpecification();

            int tryCount = 0;
            Exception lastException = null;

            Stream stream = null;
            try
            {
                stream = File.OpenRead();
                while (tryCount < 3)
                {
                    try
                    {
                        var httpClient = GetHttpClient();

                        using (var requestMessage = new HttpRequestMessage(
                                HttpMethod.Post,
                                GetChunkUriForStandardUploads()))
                        {
                            using (var multipartFormContent = new MultipartFormDataContent("upload-" + Guid.NewGuid().ToString("N")))
                            {
                                BaseRequestProvider.TryAddCookies(Client, requestMessage);

                                var streamContent = new StreamContentWithProgress(stream, OnProgress);

                                streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                                multipartFormContent.Add(streamContent, "File1", File.Name);

                                requestMessage.Content = multipartFormContent;

                                var responseMessage =
                                    httpClient.SendAsync(requestMessage, CancellationToken.None).WaitForTask();

                                MarkProgressComplete();

                                return GetUploadResponse(responseMessage);
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        lastException = exception;
                        stream.Seek(0, SeekOrigin.Begin);
                        tryCount++;
                    }
                }
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }

            throw new ApplicationException("Upload failed after 3 tries", lastException);
        }
        protected override UploadResponse InternalUpload(CancellationToken cancellationToken)
        {
            SetUploadSpecification();

            int tryCount = 0;

            while (true)
            {
                try
                {
                    TryPause();
                    if (cancellationToken.IsCancellationRequested)
                    {
                        throw new TaskCanceledException();
                    }

                    var httpClient = GetHttpClient();

                    using (var requestMessage = new HttpRequestMessage(
                               HttpMethod.Post,
                               GetChunkUriForStandardUploads()))
                    {
                        using (var streamContent = new StreamContentWithProgress(new NoDisposeStream(FileStream), progressReporter.ReportProgress, cancellationToken))
                        {
                            streamContent.TryPauseAction = TryPauseAsync;
                            requestMessage.AddDefaultHeaders(Client);
                            streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                            requestMessage.Content            = streamContent;

                            if (!UploadSpecificationRequest.Raw)
                            {
                                var multiPartContent = new MultipartFormDataContent();
                                multiPartContent.Add(streamContent, "Filedata", UploadSpecificationRequest.FileName);
                                requestMessage.Content = multiPartContent;
                            }

                            var responseMessage =
                                httpClient.SendAsync(requestMessage, CancellationToken.None).WaitForTask();

                            return(GetUploadResponse(responseMessage));
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (tryCount >= 3 || !FileStream.CanSeek || ex is TaskCanceledException)
                    {
                        throw;
                    }
                    else
                    {
                        tryCount += 1;
                        FileStream.Seek(0, SeekOrigin.Begin);
                    }
                }
            }
        }
        protected override async Task<UploadResponse> InternalUploadAsync()
        {
            int tryCount = 0;
            Stream stream = File.OpenRead();
            while (true)
            {
                try
                {
                    var httpClient = GetHttpClient();

                    using (var requestMessage = new HttpRequestMessage(
                            HttpMethod.Post,
                            GetChunkUriForStandardUploads()))
                    {
                        using (var streamContent = new StreamContentWithProgress(new NoDisposeStream(stream), OnProgress))
                        {
                            requestMessage.AddDefaultHeaders(Client);
                            streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                            requestMessage.Content = streamContent;

                            var responseMessage =
                                await
                                httpClient.SendAsync(
                                    requestMessage,
                                    CancellationToken.GetValueOrDefault(System.Threading.CancellationToken.None));

                            MarkProgressComplete();

                            return await GetUploadResponseAsync(responseMessage);
                        }
                    }
                }
                catch (Exception)
                {
                    if (tryCount >= 3 || !stream.CanSeek)
                    {
                        throw;
                    }
                    else
                    {
                        tryCount += 1;
                        stream.Seek(0, SeekOrigin.Begin);
                    }
                }
            }
        }
        public override UploadResponse Upload(Dictionary<string, object> transferMetadata = null)
        {
            SetUploadSpecification();

            int tryCount = 0;
            Stream stream = File.OpenRead();
            while (true)
            {
                try
                {
                    var httpClient = GetHttpClient();

                    using (var requestMessage = new HttpRequestMessage(
                            HttpMethod.Post,
                            GetChunkUriForStandardUploads()))
                    {
                        using (var streamContent = new StreamContentWithProgress(new NoDisposeStream(stream), OnProgress))
                        {
                            requestMessage.AddDefaultHeaders(Client);
                            streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                            requestMessage.Content = streamContent;

                            var responseMessage =
                                httpClient.SendAsync(requestMessage, CancellationToken.None).WaitForTask();

                            MarkProgressComplete();

                            return GetUploadResponse(responseMessage);
                        }
                    }
                }
                catch (Exception)
                {
                    if (tryCount >= 3 || !stream.CanSeek)
                    {
                        throw;
                    }
                    else
                    {
                        tryCount += 1;
                        stream.Seek(0, SeekOrigin.Begin);
                    }
                }
            }
        }
예제 #6
0
        private HttpRequestMessage ComposePartUpload(string chunkUploadUrl, FilePart part, string filename)
        {
            string uploadUri      = part.GetComposedUploadUrl(chunkUploadUrl);
            var    requestMessage = new HttpRequestMessage(HttpMethod.Post, uploadUri);
            var    content        = new StreamContentWithProgress(part.Bytes.GetStream(), updateProgress);

            requestMessage.Content = content;

            if (!raw)
            {
                var multiPartContent = new MultipartFormDataContent();
                content.Headers.Add("Content-Type", "application/octet-stream");
                multiPartContent.Add(content, "Filedata", filename);
                requestMessage.Content = multiPartContent;
            }

            return(requestMessage);
        }