banner



How To Upload A File On Mfc

  • Download source code (ZIP) - 275.viii KB
  • Download source lawmaking (RAR) - 236.5 KB

Introduction

Secure file transfer protocol (SFTP) is one of the approaches to uploading to the server remotely over a secure and encrypted connection. In .Internet, we can hands develop a utility to perform the mentioned task.

For this, all nosotros need to do is to use an assembly chosen SSH.Internet. SSH.NET is a Secure Shell (SSH) library for.Cyberspace, optimized for parallelism and with wide framework support.

Image 1

We tin can install this library by executing the post-obit command in package manager panel.

Install-Package SSH.NET -Version 2016.0.0

Background

In society to empathize the tip, bones knowledge/understanding of object-oriented programming is required.

Problem

I was given a job to upload the file to the remote server through schedule job. I found a few articles on the internet merely all of them were ambiguous, incomplete and not clear in vision. That's why I decided to write an article in the easiest way for the developer.

Why Prefer SFTP over HTTP

Although we can perform the same task using HTTP only it has drawbacks to utilise HTTP for file upload. HTTP is basically used for download the file or to upload small file on the server. In a normal scenario, Html form is used to submit the file and browser has timeout issue for a large file.

Coding Steps

Create a new project in Visual Studio.

Incorporate SSH.Net by executing the mentioned command in parcel manager console.

Add together the following namespaces in the file.

          using          Renci.SshNet;          using          Renci.SshNet.Sftp;

Create an object of SftpClient and provide connexion information parameter in different overload.

We tin can pass host, port, username, password directly into the constructor.

SftpClient client =          new          SftpClient (host, port, username, password);

OR

Nosotros can also create a connexion info object and pass to sftpClient'south constructor with the public key.

SftpClient sftpClient =          new          SftpClient (getSftpConnection ("          Host",          "          username", port,          "          publicKeyPath"));          public          static          ConnectionInfo getSftpConnection (string          host,          cord          username,          int          port,          string          publicKeyPath) {          return          new          ConnectionInfo(host, port, username, privateKeyObject(username, publicKeyPath)); }          private          static          AuthenticationMethod[] privateKeyObject(string          username,          string          publicKeyPath) {   PrivateKeyFile privateKeyFile =          new          PrivateKeyFile(publicKeyPath);   PrivateKeyAuthenticationMethod privateKeyAuthenticationMethod =          new          PrivateKeyAuthenticationMethod(username, privateKeyFile);          return          new          AuthenticationMethod[] { privateKeyAuthenticationMethod }; }

Connect SftpClient.

sftpClient.Connect();

Create an object of File Stream and laissez passer file path.

FileStream fs =          new          FileStream("          filePath", FileMode.Open);

You tin can set up maximum buffer size in byte.

sftpClient.BufferSize =          1024;

Upload the file.

sftpClient.UploadFile(fs, Path.GetFileName("          filePath"));

Dispose the object past calling dispose method of sftpClient once the file has uploaded.

sftpClient.Dispose ();

The file has been copied to the remote location.

Complete Lawmaking

          static          void          Chief(cord[] args)     {             Panel.WriteLine("          Create client Object");          using          (SftpClient sftpClient =          new          SftpClient(getSftpConnection                                            ("          host",          "          userName",          22,          "          filePath")))             {                 Console.WriteLine("          Connect to server");                 sftpClient.Connect();                 Console.WriteLine("          Creating FileStream object to stream a file");          using          (FileStream fs =          new          FileStream("          filePath", FileMode.Open))                 {                     sftpClient.BufferSize =          1024;                     sftpClient.UploadFile(fs, Path.GetFileName("          filePath"));                 }                 sftpClient.Dispose();             }      }          public          static          ConnectionInfo getSftpConnection (cord          host,          string          username,          int          port,          string          publicKeyPath) {          return          new          ConnectionInfo(host, port, username, privateKeyObject(username, publicKeyPath)); }          individual          static          AuthenticationMethod[] privateKeyObject(string          username,          string          publicKeyPath) {   PrivateKeyFile privateKeyFile =          new          PrivateKeyFile(publicKeyPath);   PrivateKeyAuthenticationMethod privateKeyAuthenticationMethod =          new          PrivateKeyAuthenticationMethod(username, privateKeyFile);          render          new          AuthenticationMethod[]           {         privateKeyAuthenticationMethod         }; }

Source: https://www.codeproject.com/Tips/1189835/SFTP-File-Upload-with-Csharp-Application

Posted by: smithhakis1990.blogspot.com

0 Response to "How To Upload A File On Mfc"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel