Renci.SshNet.SftpClient.BeginUploadFile C# (CSharp) Method

BeginUploadFile() public method

Begins an asynchronous uploading the stream into remote file.

Method calls made by this method to input, may under certain conditions result in exceptions thrown by the stream.

When path refers to an existing file, set canOverride to true to overwrite and truncate that file. If canOverride is false, the upload will fail and EndUploadFile(IAsyncResult) will throw an SshException.

is null. is null or contains only whitespace characters. The method was called after the client was disposed.
public BeginUploadFile ( Stream input, string path, bool canOverride, AsyncCallback asyncCallback, object state, Action uploadCallback = null ) : IAsyncResult
input Stream Data input stream.
path string Remote file path.
canOverride bool Specified whether an existing file can be overwritten.
asyncCallback AsyncCallback The method to be called when the asynchronous write operation is completed.
state object A user-provided object that distinguishes this particular asynchronous write request from other requests.
uploadCallback Action The upload callback.
return IAsyncResult
        public IAsyncResult BeginUploadFile(Stream input, string path, bool canOverride, AsyncCallback asyncCallback, object state, Action<ulong> uploadCallback = null)
        {
            CheckDisposed();

            if (input == null)
                throw new ArgumentNullException("input");

            if (path.IsNullOrWhiteSpace())
                throw new ArgumentException("path");

            var flags = Flags.Write | Flags.Truncate;

            if (canOverride)
                flags |= Flags.CreateNewOrOpen;
            else
                flags |= Flags.CreateNew;

            var asyncResult = new SftpUploadAsyncResult(asyncCallback, state);

            ThreadAbstraction.ExecuteThread(() =>
            {
                try
                {
                    InternalUploadFile(input, path, flags, asyncResult, offset =>
                    {
                        asyncResult.Update(offset);

                        if (uploadCallback != null)
                        {
                            uploadCallback(offset);
                        }

                    });

                    asyncResult.SetAsCompleted(null, false);
                }
                catch (Exception exp)
                {
                    asyncResult.SetAsCompleted(exp, false);
                }
            });

            return asyncResult;
        }

Same methods

SftpClient::BeginUploadFile ( Stream input, string path ) : IAsyncResult
SftpClient::BeginUploadFile ( Stream input, string path, AsyncCallback asyncCallback ) : IAsyncResult
SftpClient::BeginUploadFile ( Stream input, string path, AsyncCallback asyncCallback, object state, Action uploadCallback = null ) : IAsyncResult

Usage Example

Beispiel #1
0
        public bool UploadToSFTP(MemoryStream ms, string filename, bool overwrite)
        {
            string strPath = sshPath;
              if (!strPath.StartsWith("/")) {
            strPath = "/" + strPath;
              }
              if (!strPath.EndsWith("/")) {
            strPath += "/";
              }

              bool bRet = true;
              bool bWait = true;

              sshError = "";

              if (sshConnection == null || !sshConnection.IsConnected) {
            bool bResume = false;
            bool bOK = false;
            connectionDoneCallback = (bool bSuccess) => {
              bOK = bSuccess;
              bResume = true;
            };
            Connect();
            while (!bResume) System.Threading.Thread.Sleep(1);
            if (!bOK) {
              sshError = "Couldn't connect to server.";
              return false;
            }
              }

              try {
            this.ProgressBar.Start(filename, ms.Length);
            ms.Seek(0, SeekOrigin.Begin);
            IAsyncResult arUpload = null;
            AsyncCallback cbFinished = (IAsyncResult ar) => {
              bRet = true;
              bWait = false;
            };
            Action<ulong> cbProgress = new Action<ulong>((ulong offset) => {
              this.ProgressBar.Set((long)offset);
              if (this.ProgressBar.Canceled) {
            sshConnection.EndUploadFile(arUpload);
            bRet = false;
            bWait = false;
              }
            });
            try {
              arUpload = sshConnection.BeginUploadFile(ms, strPath + filename, overwrite, cbFinished, filename, cbProgress);
            } catch {
              // failed to upload, queue it for a retry after reconnecting
              sshConnection = null;
              connectionDoneCallback = (bool bSuccess) => {
            if (!bSuccess) {
              bRet = false;
              sshError = "Upload failed because couldn't connect to server.";
              bWait = false;
              return;
            }
            try {
              arUpload = sshConnection.BeginUploadFile(ms, strPath + filename, overwrite, cbFinished, filename, cbProgress);
            } catch (Exception ex) {
              bRet = false;
              sshError = "Upload failed twice: " + (ex.InnerException != null ? ex.InnerException.Message : ex.Message);
              bWait = false;
            }
              };
              Connect();
            }
              } catch (Exception ex) {
            bRet = false;
            bWait = false;
            sshError = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
            throw ex;
              }

              while (bWait) System.Threading.Thread.Sleep(1);

              this.ProgressBar.Done();
              return bRet;
        }
All Usage Examples Of Renci.SshNet.SftpClient::BeginUploadFile