Esempio n. 1
0
        public async Task UploadFile(string folderName, string fileName, Stream stream)
        {
            await TestConnection().ConfigureAwait(false);

            UploadProgress?.SetTotal(stream.Length);
            UploadProgress?.ChangeState(UploadState.PendingUpload);

            var url = await CreateNestedFoldersIfNeeded(folderName).ConfigureAwait(false);

            url += $"/{fileName}";

            var request    = CreateFtpWebRequest(url, WebRequestMethods.Ftp.UploadFile, keepAlive: true);
            var readBuffer = new byte[DefaultBufferSize];

            int count;
            var requestStream = request.GetRequestStream();

            while ((count = await stream.ReadAsync(readBuffer, 0, readBuffer.Length).ConfigureAwait(false)) != 0)
            {
                await requestStream.WriteAsync(readBuffer, 0, count).ConfigureAwait(false);

                UploadProgress?.UpdateUploaded(count);
            }

            requestStream.Flush();
            requestStream.Close();

            UploadProgress?.ChangeState(UploadState.PendingResponse);
            var response = await request.GetResponseAsync().ConfigureAwait(false);

            response.Dispose();

            UploadProgress?.ChangeState(UploadState.Done);
        }
Esempio n. 2
0
        public FtpClient(string url, int?port, string userName, string password, string certificateAsBase64,
                         string certificateFileName, UploadProgress uploadProgress = null, CancellationToken?cancellationToken = null)
            : base(uploadProgress, cancellationToken)
        {
            _url                 = url;
            _port                = port;
            _userName            = userName;
            _password            = password;
            _certificateAsBase64 = certificateAsBase64;
            _certificateFileName = certificateFileName;

            if (_url.StartsWith("ftp://", StringComparison.OrdinalIgnoreCase) == false &&
                _url.StartsWith("ftps://", StringComparison.OrdinalIgnoreCase) == false)
            {
                _url = "ftp://" + url;
            }

            if (_url.StartsWith("ftps", StringComparison.OrdinalIgnoreCase))
            {
                _useSsl = true;
                _url    = _url.Replace("ftps://", "ftp://");
            }

            if (_url.EndsWith("/") == false)
            {
                _url += "/";
            }

            Debug.Assert(_url.StartsWith("ftp://", StringComparison.OrdinalIgnoreCase));
        }
Esempio n. 3
0
 protected StorageClient(UploadProgress uploadProgress, CancellationToken?cancellationToken)
 {
     UploadProgress    = uploadProgress;
     CancellationToken = cancellationToken ?? CancellationToken.None;
 }