WebClient类在客户端处理服务器端文件中的应用

实现客户端从服务器下载文件、上传文件、删除文件,命名空间System.Net

public class ReportController
    {
        private static readonly string tempDir = Application.StartupPath + "\\temp\\";

        static ReportController()
        {
            if (!Directory.Exists(tempDir))
            {
                Directory.CreateDirectory(tempDir);
            }
        }

        /// <summary>
        /// 从服务器获取指定的报表文件
        /// </summary>
        /// <param name="url">服务器文件地址</param>
        /// <returns>本地报表文件暂存路径</returns>
        public static string LoadFromServer(string url)
        {
            try
            {
                //检验资源是否存在
                WebRequest request = WebRequest.Create(url);
                request.GetResponse();
            }
            catch
            {
                return "";
            }
            WebClient client = new WebClient();
            string fileName = url.Substring(url.LastIndexOf("/") + 1); //被下载的文件名
              string savePath = Path.Combine(tempDir, fileName);
            if (File.Exists(savePath))
            {
                File.Delete(savePath);
            }
            client.DownloadFile(url, savePath);
            client.Dispose();
            return savePath;
        }

        /// <summary>
        /// 保存本地文件到服务器
        /// </summary>
        /// <param name="fileName"></param>
        public static void SaveToServer(string fileName)
        {
            WebClient client = new WebClient();
            string cpurl = "http://localhost:4927/Handlers/DevReport/UploadReport.ashx?action=UploadFile";
            byte[] result = client.UploadFile(cpurl, Path.Combine(tempDir, fileName));
            client.Dispose();
            string rtn = System.Text.Encoding.Default.GetString(result);
        }

        /// <summary>
        /// 根据文件名称删除服务器端文件
        /// </summary>
        /// <param name="fileName"></param>
        public static void DeleteFromServer(string fileName)
        {
            WebClient client = new WebClient();
            string cpurl = "http://localhost:4927/Handlers/DevReport/UploadReport.ashx?action=DeleteFile";
            string result = client.UploadString(cpurl, fileName);
            client.Dispose();
        }
    }

服务器端使用Handler进行处理:

public class UploadReport : BaseHandler
{
    private string response = "true";
    public override void ProcessRequest(HttpContext context)
    {
        base.ProcessRequest(context);
        try
        {
            string action = context.Request["action"];
            switch (action)
            {
                case "UploadFile":
                    Process_R1(context);
                    break;
                case "DeleteFile":
                    Process_R2(context);
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {
            response = "false";
        }
        finally
        {
            context.Response.Write(response);
            context.Response.End();
        }
    }

    /// <summary>
    /// 处理客户端上传文件的请求
    /// </summary>
    /// <param name="context"></param>
    private void Process_R1(HttpContext context)
    {
        string dir = context.Server.MapPath("/") + @"ReportTemplate\";
        if (!Directory.Exists(dir))
        {
            Directory.CreateDirectory(dir);
        }
        foreach (string fileKey in context.Request.Files)
        {
            HttpPostedFile file = context.Request.Files[fileKey];
            string fileFullPath = Path.Combine(dir, file.FileName);
            if (File.Exists(fileFullPath))
            {
                File.Delete(fileFullPath);
            }
            file.SaveAs(fileFullPath);
        }
    }

    /// <summary>
    /// 处理客户端删除文件的请求
    /// </summary>
    /// <param name="context"></param>
    private void Process_R2(HttpContext context)
    {
        string dir = context.Server.MapPath("/") + @"ReportTemplate\";
        //获取客户端发送的字符串
        using (StreamReader sr = new StreamReader(context.Request.InputStream))
        {
            string fileName = sr.ReadToEnd();
            string fileFullPath = Path.Combine(dir, fileName);
            if (File.Exists(fileFullPath))
            {
                File.Delete(fileFullPath);
            }
        }
    }
    
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值