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

Advertisemen

Introduction:

I am very happy to write an article about FTP server access from windows phone programming using c#.net, there are no direct APIs available for leveraging FTP services in a Windows phone app. From an enterprise perspective, this makes it rather difficult for employees to access files over their phones. Fortunately we have socket support for windows phone to communicate with a multitude of different services, making users feel more connected and available than ever.


This article can explained about below topics:
1. What is FTP and Why?
2. How to Setup Ftp Server in Windows 8.0/8.1/10 OS system?
3. How to Connect FTP Server from WindowsPhone?
4. Upload files to Ftp 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 FTP and Why?

  • The File Transfer Protocol (FTP) is a standard network protocol used to transfer computer files from one host to another host over a TCP-based network, such as the Internet.
  •  FTP is built on a client-server architecture and uses separate control and data connections between the client and the server.
  • FTP users may authenticate themselves with a clear-text sign-in protocol, normally in the form of a username and password, but can connect anonymously if the server is configured to allow it.
  • For secure transmission that protects the username and password, and encrypts the content, FTP is often secured with SSL/TLS (FTPS). SSH File Transfer Protocol (SFTP) is sometimes also used instead, but is technologically different.
2. How to Setup Ftp Server in Windows 8.0/8.1/10 OS system?

This section is not applicable for windows app developer, but it is better to learn understand the FTP communication by creating server in your machine.
2.1. So open Programs and Features in Control panel. Click on ‘Turn Windows features on or off’ as shown below.

If IIS was not installed earlier on particular Windows 8 or 8.1 computer, you need to install other features of IIS too ( as shown by arrow marks). See the below screenshot for the actual requirements to run FTP server on Windows 8/8.1 (All features which are ticked need to be installed).

Note: You must be restart your machine for above step to make changes in your . 

2.2. After installation is completed, search and open ‘Internet Information Services (IIS) Manager’ in Administrator mode from start menu.


Expand the sites, right click on it and ‘Add FTP Site’

2.3. Give a name for FTP site and browse the local folder which you need to give access to others through server.(I already created a folder called 'FTPShare' on C drive before reaching this step.)


2.4. In next screen you need to select the local computer’s IP address from drop down box. I hope you have already set up static IP for the computer.


Under SSL option, select No SSL to make the connection without SSL certificate. In production environment for professional FTP server setup, you may need to enable SSL which requires a certificate.

Now its time to Set Up FTP Access Permission on Windows 8 or 8.1 or Windows 10

2.5. In next screen you can set the permission for users to access the FTP site. Here you need to decide how others will be accessing the FTP share and who will be having Read-only or Read & Write access.

Let’s assume this scenario, you want specific users to have read and write access, so obviously they must type an user name and password for it. Other users can access the FTP site without any username or password to view the content only, it’s called anonymous users access.

You will find several ways to do this, but here is the simple way which worked for me.

Firstly open start menu and type 'lusrmgr.msc', and create a user on Windows 8 or Windows 10 local computer (if you are not using active directory environment) .


and add the users who will be having read and write access on FTP site. In this example I have created ‘ftpusers’ group and added required users inside it.

2.6. Now, let’s continue the FTP site settings. In next screen to give the permission, select ‘Basic’ which will be prompting for user name and password, select Specified  roles or groups option from drop down and type the correct group name. Set Read and Write permission for the group.
Press Finish to complete the setup.
Check the Firewall ! It should allow FTP traffic.

We are almost done and completed the all necessary steps. Now, you need to either disable the firewall or allow FTP inbound and outbound traffic on Windows 8 or 8.1 computer. I hope you can do it easily.

3. How to Connect FTP Server from WindowsPhone?

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 :WindowsPhoneFTP)
Step 2:
Recently i made one binary file for FTP on windows phone programming, and temporarily now i can directly add ftp binary file to reference folder to access FTP server using windows phone 8.0 silverlight C#.
Step 3: 
Now add below helper class for uploading files to FTP server.
  1. public class FtpClientClass  
  2.     {  
  3.         private static string m_Host = "Please ENTER your Server Name/IP";  
  4.         private static string FtpUserName = "Server UserName";  
  5.         private static string FtpPwd = "Server Password";  
  6.         private static string FtpPort = "21";  
  7.         private readonly NetworkCredential m_Credentials = new NetworkCredential(FtpUserName, FtpPwd);  
  8.   
  9.         private const string PathToSet = "/";  
  10.   
  11.         private const string FolderToCreate = "";//"WindowsPhone"; 
  12.         public FtpClient CreateFtpClient()  
  13.         {  
  14.             
  15.             var ftpClient = new FtpClient();  
  16.             ftpClient.Credentials = m_Credentials;  
  17.             ftpClient.HostName = new HostName(m_Host);  
  18.             ftpClient.ServiceName = FtpPort;  
  19.   
  20.             return ftpClient;  
  21.         } 
  22.    
  23.         //Upload file to Ftp Server 
  24.         public async Task<string> UploadFileAsync(string DataFields, string fileUsername)  
  25.         {  
  26.             try  
  27.             {  
  28.                 if (fileUsername=="")  
  29.                 {  
  30.                     fileUsername = "NoUserName";  
  31.                 }  
  32.                 System.Diagnostics.Debug.WriteLine("-----------------------------------");  
  33.                 System.Diagnostics.Debug.WriteLine("TestCreateFileAsync");  
  34.                 System.Diagnostics.Debug.WriteLine("-----------------------------------");  
  35.   
  36.                 var ftpClient = CreateFtpClient();  
  37.   
  38.                 await ftpClient.ConnectAsync();  
  39.                 await ftpClient.CreateDirectoryAsync(PathToSet + FolderToCreate);  
  40.                 string testFilePath = PathToSet + fileUsername + ".txt";  
  41.   
  42.                 using (var stream = await ftpClient.OpenWriteAsync(testFilePath))  
  43.                 {  
  44.                     var bytes = Encoding.UTF8.GetBytes(DataFields);  
  45.   
  46.                     await stream.WriteAsync(bytes.AsBuffer());  
  47.                     await stream.FlushAsync();  
  48.                 }  
  49.                 //await ftpClient.DisconnectAsync();  
  50.                 return "Success";  
  51.             }  
  52.             catch(Exception ex)  
  53.             {  
  54.                 return ex.Message;  
  55.             }  
  56.         }  
  57.     }  
Now we can use above helper class like below:
  • using System;  
  • using System.Collections.Generic;  
  • using System.Linq;  
  • using System.Net;  
  • using System.Windows;  
  • using System.Windows.Controls;  
  • using System.Windows.Navigation;  
  • using Microsoft.Phone.Controls;  
  • using Microsoft.Phone.Shell;  
  • using WindowsPhoneFTP.Resources;  
  •   
  • namespace WindowsPhoneFTP  
  • {  
  •     public partial class MainPage : PhoneApplicationPage  
  •     {  
  •         // Constructor  
  •         public MainPage()  
  •         {  
  •             InitializeComponent();  
  •         }  
  •         private void BtnSubmit_Click(object sender, RoutedEventArgs e)  
  •         {  
  •             UploadFile_FTP();  
  •         }  
  •         private async void UploadFile_FTP()  
  •         {  
  •             FtpClientClass ObjFtpClientClass = new FtpClientClass();  
  •             var Result = await ObjFtpClientClass.UploadFileAsync("Hi My FileText""MyFile");  
  •             TbckUploadStatus.Text = "File upload: "+Result;
  •         }  
  •   
  •     }  
  • }  

  • 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 FTP" Background="#FF58E277" Click="BtnSubmit_Click"/>  
    9.         <TextBlock Name="TbckUploadStatus" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Bottom" Foreground="Black"/>  
    10.         </Grid>  
    11.     </Grid>  

    Output:
    WindowsPhoneFTP

    If you want to access SFTP server from windows phone silverlight, you can read it on next article from here.

    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  :)


    Advertisemen

    Disclaimer: Gambar, artikel ataupun video yang ada di web ini terkadang berasal dari berbagai sumber media lain. Hak Cipta sepenuhnya dipegang oleh sumber tersebut. Jika ada masalah terkait hal ini, Anda dapat menghubungi kami disini.

    Tidak ada komentar:

    Posting Komentar

    © Copyright 2017 Tutorial Unity 3D