C# WebClient 多文件上传实现

public static void sendRecord2Server(DirectoryInfo di)
        {


            DirectoryInfo[] ds = di.GetDirectories();
            foreach (DirectoryInfo d in ds)
            {
                sendRecord2Server(d);
            }

            ServerAddr s = getServerAddr();

            FileInfo[] fs = di.GetFiles();

            if (fs.Length == 0)
            {
                return;
            }
            WebClient wc = new WebClient();

            string url = "http://" + s.ip + ":" + s.port + "/" + Consts.WebServerProjectName + "/uploadRecord";

            string boundary = headers(wc);
            byte[] data = sendBatchRecordFile(fs, boundary);

            byte[] resp = uploadData(wc, url, data);


            string res = Encoding.UTF8.GetString(resp);
            Result r = json2Result(res);

            if (r != null && r.Success)
            {
                foreach (FileInfo f in fs)
                {
                    f.MoveTo(Consts.OldRecordDirectory + "\\" + f.Name);
                    //f.Delete();
                }

            }
            else
            {
                throw new Exception("定时:考勤数据文件上传失败!");
            }
            //}
            wc.Dispose();

            

        }

        private static string headers(WebClient wc)
        {
            Guid g = Guid.NewGuid();
            string boundary = g.ToString();
            boundary = boundary.Replace("-", "");
            boundary = "----" + boundary;
            //MessageBox.Show("boundary is :" + boundary);

            wc.Headers.Add("Content-Type", "multipart/form-data; boundary=" + boundary);

            return boundary;
        }

        private static byte[] sendBatchRecordFile(FileInfo[] fs, string boundary)
        {
            byte[] data = null;
            int len = 0;
            if (fs.Length < 10)
            {
                len = fs.Length;
            }
            else
            {
                len = fs.Length;
            }

            string d = "";
            for (int i = 0; i < len; i++)
            {

                using (StreamReader sr = new StreamReader(fs[i].FullName))
                {
                    string part = partHeader(boundary, "recordFile", fs[i].Name, "text/plain");
                    string line = "";
                    while ((line = sr.ReadLine()) != null)
                    {
                        part += line + "\r\n";
                    }
                    d += part;
                }

                d += "\r\n";
            }
            d += "\r\n" + partEnd(boundary);
            //MessageBox.Show(d);
            data = utf8(d);
            return data;
        }

  private static string partHeader(string boundary, string name, string filename, string contentType)
        {
            boundary = "--" + boundary + "\r\n";
            string partHeader = "Content-Disposition: form-data; name=" + name + "; filename=" + filename + "\r\n";
            contentType = "Content-Type: " + contentType + "\r\n\r\n";

            partHeader = boundary + partHeader + contentType;
            return partHeader;

        }
        private static byte[] utf8(string s)
        {
            return Encoding.UTF8.GetBytes(s);
        }
        private static string partEnd(string boundary)
        {
            boundary = "--" + boundary + "--";
            return boundary;
        }

        private static byte[] uploadData(WebClient wc, string url, byte[] data)
        {
            return wc.UploadData(url, "post", data);
        }

C# WebClient 多文件上传实现_第1张图片



你可能感兴趣的:(Windows,HTTP)