• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#WebClient的文件上传下载

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 上传文件

 

   string path = openFileDialog1.FileName;
                WebClient wc = new WebClient();
                wc.Credentials = CredentialCache.DefaultCredentials;
                wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                wc.QueryString["fname"] = openFileDialog1.SafeFileName;
                byte[] fileb = wc.UploadFile(new Uri(@"http://192.168.31.37:8977/FileHandler.ashx"), "POST", path);
                string res = Encoding.GetEncoding("gb2312").GetString(fileb);

                  //或者
                //wc.UploadFileAsync(new Uri(@"http://192.168.31.37:8977/FileHandler.ashx"), "POST", path);
                //wc.UploadFileCompleted += (s, es) =>
                //{
                //    MessageBox.Show(Encoding.GetEncoding("gb2312").GetString(es.Result));
                //};
        

 服务端处理请求

 public class FileHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            try
            { 
            HttpFileCollection files = context.Request.Files;
            if (files.Count > 0)
            {
                files[0].SaveAs(HttpContext.Current.Server.MapPath("files/" + context.Request.QueryString["fname"]));
                context.Response.Write("save success!");
            }
            else
                context.Response.Write("hello request!");
            }
            catch (Exception ex)
            {
                context.Response.Write("save error!" + ex.Message);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

下载文件

  if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                string path = folderBrowserDialog1.SelectedPath;
                WebClient wc = new WebClient();
                //wc.Credentials = CredentialCache.DefaultCredentials;
                wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                string fileUrl = @"http://192.168.31.37:8977/files/多用文本.txt";

                wc.DownloadFile(new Uri(fileUrl), string.Format(@"{0}\{1}", path, fileUrl.Substring(fileUrl.LastIndexOf('/') + 1)));
                //或者
                wc.DownloadFileCompleted += (s, es) =>
                 {
                     MessageBox.Show("下载成功!");
                 };

                wc.DownloadFileAsync(new Uri(fileUrl), string.Format(@"{0}\{1}", path, fileUrl.Substring(fileUrl.LastIndexOf('/') + 1)));
            }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
装饰模式(Decorator)C++实现发布时间:2022-07-13
下一篇:
[C#网络编程系列]专题七:UDP编程补充——UDP广播程序的实现发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap