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.

If the remote file already exists, it is overwritten and truncated.

is null. is null or contains only whitespace characters. Client is not connected. Permission to list the contents of the directory was denied by the remote host. -or- A SSH command was denied by the server. A SSH error where is the message from the remote host. The method was called after the client was disposed.
public BeginUploadFile ( Stream input, string path ) : IAsyncResult
input Stream Data input stream.
path string Remote file path.
return IAsyncResult
        public IAsyncResult BeginUploadFile(Stream input, string path)
        {
            return BeginUploadFile(input, path, true, null, null);
        }

Same methods

SftpClient::BeginUploadFile ( Stream input, string path, AsyncCallback asyncCallback ) : IAsyncResult
SftpClient::BeginUploadFile ( Stream input, string path, AsyncCallback asyncCallback, object state, Action uploadCallback = null ) : IAsyncResult
SftpClient::BeginUploadFile ( Stream input, string path, bool canOverride, 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