none
Example of C# code for FTP uploads & downloads of files

    Question

  • I am stumbling across a lot of old code which I don't think will work in Window Store Apps because it is not Async (and the newer Async logic not older stuff..)  Like I thought I found it using FTPWebRequest...  But, this is not valid in the Windows Store apps.. There is something called WebRequest  but I haven't found a good example of how to do simple uploads & downloads with it, and since it doesn't have a prefix of FTP, this may not even be the correct newer process..

    Can anyone point me to good examples,  or post something?? Thanks...


    Wenda

    Thursday, July 11, 2013 12:55 AM

Answers

  • You can use the BackgroundTransfer API to download from an FTP site. See the Background Transfer sample (Windows 8) .

    For upload you can use WebRequest to send FTP commands, but they aren't specifically built in. The Download FTP file in Windows Store app OneCode sample demonstrates doing this for download and the upload case should be similar. For most straight download scenarios (without browsing) BackgroundTransfer will be the better choice.

    --Rob
    Thursday, July 11, 2013 1:38 AM
  • Ok  I think I have tracked down something..

     
           private async void DownloadFTPFileAsync(
                BackgroundDownloader downloader,
                NetworkCredential credential,
                FTPFileSystem item,
                StorageFile targetFile)
            {
                if (item.Size > 1048576) // 1M Byte
                {
                    Progress<DownloadOperation> progressCallback = new Progress<DownloadOperation>(DownloadProgress);
    
                    Uri urlWithCredential = item.Url;
    
                    if (credential != null)
                    {
                        urlWithCredential = new Uri(item.Url.ToString().ToLower().Replace(@"ftp://",
                            string.Format(@"ftp://{0}:{1}@", 
                            FTPFileSystem.EncodeUrl(credential.UserName), 
                            FTPFileSystem.EncodeUrl(credential.Password))));
                    }
    
                    DownloadOperation download = downloader.CreateDownload(
                        urlWithCredential,
                        targetFile);
                    ActiveBackgroundDownloaders.Add(download);
                    await download.StartAsync().AsTask(progressCallback);
                }
                else
                {
                    await FTPClient.DownloadFTPFileAsync(item, targetFile, credential)
                        .ContinueWith(new Action<Task<DownloadCompletedEventArgs>>(DownloadProgress));
                }
    
            }

                      


    Wenda

    Thursday, July 11, 2013 11:16 AM

All replies

  • You can use the BackgroundTransfer API to download from an FTP site. See the Background Transfer sample (Windows 8) .

    For upload you can use WebRequest to send FTP commands, but they aren't specifically built in. The Download FTP file in Windows Store app OneCode sample demonstrates doing this for download and the upload case should be similar. For most straight download scenarios (without browsing) BackgroundTransfer will be the better choice.

    --Rob
    Thursday, July 11, 2013 1:38 AM
  • Thanks I downloaded it and will need to study this..  With the hardcoded addresses that don't work and are http// addresses (rather then ftp//) which also don't ask for username or password, I am hoping it is complete enough to direct me through.. But it also points me to some new key words to surf the net in a look for more information if it leave me with gaps & holes to fill in..


    Wenda


    • Edited by moosetracker Thursday, July 11, 2013 9:10 AM spell check
    Thursday, July 11, 2013 9:09 AM
  • I've looked it over and also looked at the classes that are offered by the Background Transfer,  I don't see it offering the ability to download from an FTP site, just downloading from a website..  Do you have any other information that will point me in the right direction?

    Wenda

    Thursday, July 11, 2013 10:51 AM
  • Ok  I think I have tracked down something..

     
           private async void DownloadFTPFileAsync(
                BackgroundDownloader downloader,
                NetworkCredential credential,
                FTPFileSystem item,
                StorageFile targetFile)
            {
                if (item.Size > 1048576) // 1M Byte
                {
                    Progress<DownloadOperation> progressCallback = new Progress<DownloadOperation>(DownloadProgress);
    
                    Uri urlWithCredential = item.Url;
    
                    if (credential != null)
                    {
                        urlWithCredential = new Uri(item.Url.ToString().ToLower().Replace(@"ftp://",
                            string.Format(@"ftp://{0}:{1}@", 
                            FTPFileSystem.EncodeUrl(credential.UserName), 
                            FTPFileSystem.EncodeUrl(credential.Password))));
                    }
    
                    DownloadOperation download = downloader.CreateDownload(
                        urlWithCredential,
                        targetFile);
                    ActiveBackgroundDownloaders.Add(download);
                    await download.StartAsync().AsTask(progressCallback);
                }
                else
                {
                    await FTPClient.DownloadFTPFileAsync(item, targetFile, credential)
                        .ContinueWith(new Action<Task<DownloadCompletedEventArgs>>(DownloadProgress));
                }
    
            }

                      


    Wenda

    Thursday, July 11, 2013 11:16 AM