Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Windows Store Apps > Windows Store and Network (C#) > Windows Store Apps and Download Using FTP Server (C#)



Clound SSD Virtual Server

Windows Store Apps and Download Using FTP Server (C#)

Windows Store Apps and Download Using FTP Server (C#) อีกความสามารถหนึ่งของ WinRT API คือการ Access เข้าไปใน Protocol FTP ที่ใช้สำหรับการ Transfer ไฟล์ทั้ง Download และ Upload ระหว่าง Client กับ Server ซึงจะมีประสิทธิภาพมากกว่าการ Download ไฟล์ผ่าน http คือ ftp จะมีความเป็นส่วนตัว และปลอดภัยมากกว่า สามารถ Transfer file ได้ในปริมาณที่มาก Size ที่ใหญ่ และรองรับการ Transfer file ได้ในครั้งล่ะมาก ๆ พร้อมกัน

Windows Store Apps and Download Using FTP Server (C#)

Windows Store Apps and Download Using FTP Server (C#)


ในการ Transfer ไฟล์ผ่าน FTP ก็จะเหมือนกับ HTTP คือจะต้องมี path ของ ftp ซึ่งปกติจะอยู่ในรูปแบบ ftp:// ตามด้วย URL อาจจะเป็น Domain หรือ IP Address โดย FTP มี Port ที่เป็นมาตรฐานคือ 21 ฉะนั้นถ้า Server กำหนดเป็น 21 ก็สามารถเรียกได้เลยโดยไม่ต้องระบุชื่อ Port

ตัวอย่างการเขียน Windows Store Apps เพื่อ Download file จาก FTP Server

ในการ Download ขั้นเราจะต้องทราบแหล่งที่มาของไฟล์ FTP หรือ Path ของไฟล์ ซึ่งก็จะมีรูปแบบเหมือนกับ http เพียงแต่เปลี่ยนเป็น ftp:// เช่น

ftp://thaicreate.com/myfile/SourceFile.jpg

และโดยปกติแล้วการ Transfer file ระหว่าง FTP จะต้องมี Username และ Password ซะก่อน สรุปแล้วคือ จะต้องมี Path ของ FTP / User / Password








Using ตัว Class ที่จำเป็นจะต้องใช้คือ BackgroundTransfer , Storage และ Credentials
using Windows.Networking.BackgroundTransfer;
using Windows.Storage;
using Windows.Security.Credentials;

ตัวอย่าง FTP Path และ Destination ที่จะจัดเก็บลงใน Storage
string sourceFTP = "ftp://thaicreate.com/myfile/SourceFile.jpg";
string destinationFile = "Myfile.jpg";

การ Login เข้าสู่ FTP ซึ่งจะต้องมี Username และ Password ในการ Login
var file = await KnownFolders.DocumentsLibrary.CreateFileAsync(destinationFile, CreationCollisionOption.ReplaceExisting);

var pc = new PasswordCredential();
pc.UserName = "thaicreate_ftp";
pc.Password = "password";

เริ่มต้นการ Download ไฟล์
var downloader = new BackgroundDownloader();
var downloadOperation = downloader.CreateDownload(new Uri(sourceFTP), file);

ลองมาดูตัวอย่างแบบเต็ม ๆ

Example ตัวอย่างการเขียน Windows Store Apps เพื่อ Download ไฟล์จาก FTP Server

Windows Store Apps and Download Using FTP Server (C#)

ออกแบบหน้าจอดังรูป ประกอบด้วย ProgressBar และ Button จากนั้นเขียน Code ทั้งหมดดังนี้

MainPage.xaml
<Page
    x:Class="WindowsStoreApps.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WindowsStoreApps"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Button Content="Download" x:Name="btnDownload" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="317,255,0,0" Width="177" Click="btnDownload_Click" FontSize="20"/>
        <ProgressBar x:Name="progressPercent" HorizontalAlignment="Left" Height="46" Margin="94,162,0,0" VerticalAlignment="Top" Width="632"/>

    </Grid>

</Page>

MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Devices.Geolocation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

using Windows.Foundation;
using Windows.Networking.BackgroundTransfer;
using Windows.Storage;
using Windows.Security.Credentials;


// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace WindowsStoreApps
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    /// 
    public sealed partial class MainPage : Page
    {

        public MainPage()
        {
            this.InitializeComponent();
        }

        private async void btnDownload_Click(object sender, RoutedEventArgs e)
        {

string sourceFTP = "ftp://thaicreate.com/myfile/SourceFile.jpg";
string destinationFile = "Myfile.jpg";

var file = await KnownFolders.DocumentsLibrary.CreateFileAsync(destinationFile, CreationCollisionOption.ReplaceExisting);

var pc = new PasswordCredential();
pc.UserName = "thaicreate_ftp";
pc.Password = "password";

var downloader = new BackgroundDownloader();
var downloadOperation = downloader.CreateDownload(new Uri(sourceFTP), file);

            var progressCallback = new Progress<DownloadOperation>(op =>
            {
                if (op.Progress.TotalBytesToReceive > 0)
                {
                    Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    {
                        this.progressPercent.Value = op.Progress.BytesReceived * 100 / op.Progress.TotalBytesToReceive;
                    });

                }
            });

            await downloadOperation.StartAsync().AsTask(progressCallback);

            var response = downloadOperation.GetResponseInformation();

        }

    }
}

Result

Windows Store Apps and Download Using FTP Server (C#)

กรณีมี Error ดังรูป

Windows Store Apps and Download Using FTP Server (C#)

อย่าลืมกำหนด Permission พวก Internet และ Network ดังรูป

Windows Store Apps and Download Using FTP Server (C#)

ทดสอบการทำงาน

เพิ่มเติม กรณีที่ต้องการจัดเก็บลงใน Storage ให้เพิ่ม Code ดังนี้

            var targetFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(destinationFile);
            await file.MoveAndReplaceAsync(targetFile);


Code เต็ม ๆ

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Devices.Geolocation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

using Windows.Foundation;
using Windows.Networking.BackgroundTransfer;
using Windows.Storage;
using Windows.Security.Credentials;


// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace WindowsStoreApps
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    /// 
    public sealed partial class MainPage : Page
    {

        public MainPage()
        {
            this.InitializeComponent();
        }

        private async void btnDownload_Click(object sender, RoutedEventArgs e)
        {

            string sourceFTP = "ftp://thaicreate.com/myfile/SourceFile.jpg";
            string destinationFile = "Myfile.jpg";

            var file = await KnownFolders.DocumentsLibrary.CreateFileAsync(destinationFile, CreationCollisionOption.ReplaceExisting);

            var pc = new PasswordCredential();
            pc.UserName = "thaicreate_ftp";
            pc.Password = "password";

            var downloader = new BackgroundDownloader();
 
            var downloadOperation = downloader.CreateDownload(new Uri(sourceFTP), file);

            var progressCallback = new Progress<DownloadOperation>(op =>
            {
                if (op.Progress.TotalBytesToReceive > 0)
                {
                    Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    {
                        this.progressPercent.Value = op.Progress.BytesReceived * 100 / op.Progress.TotalBytesToReceive;
                    });

                }
            });

            await downloadOperation.StartAsync().AsTask(progressCallback);

            var response = downloadOperation.GetResponseInformation();

            var targetFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(destinationFile);
            await file.MoveAndReplaceAsync(targetFile);
        }

    }
}


Windows Store Apps and Download Using FTP Server (C#)

หลังจากที่ Download เสร็จแล้ว ก็จะมีการจัดเก็บลงใน Storage เที่เราได้กำหนดไว้







.

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2014-06-23 13:19:26 / 2017-03-19 15:12:14
  Download : No files
 Sponsored Links / Related

 
Windows Store Apps and Send Mail Using SMTP Server (C#)
Rating :

 
Windows Store Apps and Send Message / SMS (C#)
Rating :

 
Windows Store Apps and Timer / Trigger / Thread (C#)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 00
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่