简体   繁体   中英

How to upload file to ftp server in metro app using c#

I want to upload files to FTP sever in metro application using C#. And I tried this one but it's not working.

I created here to upload small .png file to sever but it's not working.

public static async Task<bool> UpLoad(string localsubfolders, string ftpURIInfo, string Username, string Password, string filename)
    {


        bool Successful = false;
        try
        {


            BackgroundUploader uploader = new BackgroundUploader();

            StorageFolder storageFolder =  Windows.Storage.ApplicationData.Current.LocalFolder;
            StorageFile sampleFile =  await storageFolder.GetFileAsync("wp_ss_20150313_0001.png");



            Uri urlWithCredential;
            bool Success = Uri.TryCreate(ftpURIInfo + "" + filename, UriKind.Absolute, out urlWithCredential);

            if (!string.IsNullOrEmpty(Username.Trim()) &&
               !string.IsNullOrEmpty(Password.Trim()))
            {
                urlWithCredential = new Uri(urlWithCredential.ToString().ToLower().Replace(@"ftp://",
                    string.Format(@"ftp://{0}:{1}@",
                    Username,
                    Password)));
            }

            UploadFile(urlWithCredential, sampleFile);

        }
        catch (Exception)
        {
            throw;
        }
        return Successful;
    }



    public static async Task UploadFile(Uri destination, StorageFile targetFile)
    {
        var request = WebRequest.Create(destination);
        request.Credentials = Credentials;
        request.Method = "STOR";
        try
        {
            using (var requestStream = (await request.GetRequestStreamAsync()))
            using (var stream = await targetFile.OpenStreamForReadAsync())
            {
                stream.CopyTo(requestStream);
            }
        }
        catch { }
    }

i find the solution for this problem

following codes are working fine .

public static async Task<bool> SmallUpload1(string ftpURIInfo, string filename, string username, string password)
    {

        string serverUrl;
        Uri serverUri = null;
        //StorageFolder localFolderArea;
        NetworkCredential credential;
        bool Successful = false;

        try
        {

            StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
            StorageFile targetFile = await storageFolder.GetFileAsync("wp_ss_20150313_0001.png");





            Uri.TryCreate(ftpURIInfo, UriKind.Absolute, out serverUri);
            serverUrl = serverUri.ToString();
            credential = new System.Net.NetworkCredential(username.Trim(),
                password.Trim());

            WebRequest request = WebRequest.Create(serverUrl + "/" + filename);
            request.Credentials = credential;

            request.Proxy = WebRequest.DefaultWebProxy;

            //STOR is for ftp: // POST is for http:// web sites
            request.Method = "STOR";

            byte[] buffer = Encoding.UTF8.GetBytes(UploadLine);




            using (Stream requestStream = await request.GetRequestStreamAsync())
            {
                using (var stream = await targetFile.OpenStreamForReadAsync())
                {
                    stream.CopyTo(requestStream);
                }
            }

            Successful = true;

        }
        catch (Exception)
        {

            throw;
        }
        return Successful;

    } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM