Google WindowsPhone Silverlight (8.0 & 8.1) : Upload files to SFTP Server (C# - XAML) | SubramanyamRaju Xamarin & Windows App Dev Tutorials

Sunday, 27 December 2015

WindowsPhone Silverlight (8.0 & 8.1) : Upload files to SFTP Server (C# - XAML)

Introduction:

In previous article, I was explained about "Uploading files to FTP server" and this article can explain about "How to access SFTP server from windows phone programming"

This article can explained about below topics:
1. What is SFTP and Why?
2. How to Setup SFTPServer in Windows 8.0/8.1/10 OS system?
3. How to Connect SFTP Server from WindowsPhone?
4. Upload files to SFTP Server

Requirements:

  • This sample is targeted for windowsphone silverlight 8.1 OS,So make sure you’ve downloaded and installed the Windows Phone 8.1 SDK. For more information, see Get the SDK.
  • I assumes that you’re going to test your app on the Windows Phone emulator. If you want to test your app on a phone, you have to take some additional steps. For more info, see Register your Windows Phone device for development.
  • This post assumes you’re using Microsoft Visual Studio Express 2013 for Windows or Later.

Description:

1. What is SFTP and Why?
  • FTP, or "File Transfer Protocol" is a popular method of transferring files between two remote systems.
  • SFTP, which stands for SSH File Transfer Protocol, or Secure File Transfer Protocol, is a separate protocol packaged with SSH that works in a similar way over a secure connection. The advantage is the ability to leverage a secure connection to transfer files and traverse the filesystem on both the local and remote system.
In almost all cases, SFTP is preferable to FTP because of its underlying security features and ability to piggy-back on an SSH connection. FTP is an insecure protocol that should only be used in limited cases or on networks you trust.

Although SFTP is integrated into many graphical tools, this guide will demonstrate how to use it through its interactive command line interface.

2. How to Setup SFTP Server in Windows 8.0/8.1/10 OS system?
It is very easy to setup SFTP server, and please get guidance from WinSCP official website from here

3. Upload files to SFTP server
Fortunately for windows phone silverlight, We already Have SSH.NET library which support to access SFTP server from windows phone. But still there is little bit issues while SFTP connections. And after lots of research finally i made working binary(DLL) file for windows phone silverlight. 
Step 1:
1. Open Microsoft Visual Studio Express 2013 for Windows (or) later.
2. Create new silverlight project using the "Blank App" template available under Visual C# -> Store Apps -> Windows Phone Apps. (for example project name :WindowsPhoneSFTP)
Step 2:
Recently i made one binary file for SFTP on windows phone programming, and temporarily now i can directly add SFTP binary file to reference folder to access SFTP server using windows phone 8.0 silverlight C#.
Step 3: 
Now add below method for uploading files to SFTP server.
  • public async Task<string> SFTPFileUpload(string FileContent, string fileUsername)  
  •         {  
  •             try  
  •             {  
  •                 string FolderPath = "FTPFiles/WindowsPhone";  
  •                 string m_Host = "Server Name/IP Address";  
  •                 string FtpUserName = "SFTP server username";  
  •                 string FtpPwd = "SFTP Password";  
  •                 int SFtpPort = 22;//Default IP address  
  •                 fileUsername = fileUsername + ".txt";  
  •                 SftpClient sftp = new SftpClient(m_Host, SFtpPort, FtpUserName, FtpPwd);  
  •                 sftp.ConnectionInfo.Timeout = TimeSpan.FromSeconds(6000);  
  •   
  •                 sftp.Connect();  
  •                 sftp.ChangeDirectory(FolderPath);  
  •                 byte[] data = Encoding.Unicode.GetBytes(FileContent);  
  •                 sftp.BufferSize = 4 * 1024;  
  •   
  •                 Stream fileStream = new MemoryStream(data);  
  •   
  •                 sftp.UploadFile(fileStream, fileUsername, null);  
  •   
  •                 return "Success";  
  •             }  
  •             catch (Exception ex)  
  •             {  
  •                 ShellToast toast = new ShellToast();  
  •                 toast.Title = ex.Message.ToString();  
  •                 toast.Content = DateTime.Now.ToString();  
  •                 toast.Show();  
  •                 return ex.Message;// "Server is not connected.";  
  •             }  
  •         }  

  • Now we can use above method like below:
    1. private async void BtnSubmit_Click(object sender, RoutedEventArgs e)  
    2.         {  
    3.             var Result = await SFTPFileUpload("Hi My FileText""MyFile");  
    4.             TbckUploadStatus.Text = "File upload: " + Result;  
    5.         }  

    And add below xaml code in MainPage.xaml
    1. <!--LayoutRoot is the root grid where all page content is placed-->  
    2.     <Grid x:Name="LayoutRoot" Background="White">  
    3.         <Grid VerticalAlignment="Center">  
    4.         <Grid.RowDefinitions>  
    5.             <RowDefinition Height="Auto"/>  
    6.             <RowDefinition Height="*"/>  
    7.         </Grid.RowDefinitions>  
    8.         <Button Name="BtnSubmit" VerticalAlignment="Center" Content="Upload to SFTP" Background="#FF58E277" Click="BtnSubmit_Click"/>  
    9.         <TextBlock Name="TbckUploadStatus" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Bottom" Foreground="Black"/>  
    10.         </Grid>  
    11.     </Grid>  

    Output:

    WindowsPhoneSFTP
    FeedBack Note:

    Please share your thoughts,what you think about this post,Is this post really helpful for you?I always welcome if you drop comments on this post and it would be impressive.

    Follow me always at  
    Have a nice day by  :)

    No comments:

    Post a Comment

    Search Engine Submission - AddMe