⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uploader.cs

📁 音像资料管理系统光盘制作控件源代码音像资料管理系统光盘制作控件源代码
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;

namespace IsoFtp
{
    /// <summary>
    /// 基于WebClient实现的FTP协议上传文件的工具类。
    /// </summary>
    public class FtpUploader
    {
        /// <summary>将指定的本地文件上传到远程目标 FTP 服务器特定 URL 上。</summary>
        /// <param name="p_fileName">指定的本地文件名。</param>
        /// <param name="p_targetUrl">远程目标 FTP 服务器目录的 URL。URL 必须以“ftp://”开头。</param>
        /// <param name="p_userName">登陆 FTP 服务器的用户名。</param>
        /// <param name="p_password">登陆 FTP 服务器的密码</param>
        public static void Upload(string p_fileName, string p_targetUrl, string p_userName, string p_password)
        {
            // 效验文件是否存在。
            if (!File.Exists(p_fileName))
            {
                throw new Exception(string.Format("文件“{0}”不存在。"));
            }

            // 效验 URL 是否合法。
            Uri targetUrl = new Uri(p_targetUrl);
            if (targetUrl.Scheme != Uri.UriSchemeFtp)
            {
                throw new Exception(string.Format("“{0}”不是一个合法的 FTP URL。", p_targetUrl));
            }
            if (targetUrl.IsFile)
            {
                throw new Exception(string.Format("“{0}”指向的是远程文件,而不是远程目录。"));
            }

            WebClient ftpClient = new WebClient();
            ftpClient.Credentials = new NetworkCredential(p_userName, p_password);
            Console.WriteLine("正在上传文件...");
            p_targetUrl = p_targetUrl + "/" + new System.IO.FileInfo(p_fileName).Name;
            ftpClient.UploadFile(p_targetUrl, p_fileName);
            ftpClient.UploadProgressChanged += new UploadProgressChangedEventHandler(UploadProgressCallback);

            Console.WriteLine(string.Format("文件已上传至“{0}”。", p_targetUrl));
        }
        private static void UploadProgressCallback(object sender, UploadProgressChangedEventArgs e)
        {
            // Displays the operation identifier, and the transfer progress.
            Console.WriteLine("{0}    uploaded {1} of {2} bytes. {3} % complete...",
                (string)e.UserState,
                e.BytesSent,
                e.TotalBytesToSend,
                e.ProgressPercentage);
        }

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -