コード例 #1
0
        private void btnExtWebClientUpload_Click(object sender, EventArgs e)
        {
            Uri uri = new Uri(txtUrl.Text);
            // 準備檔案
            bool done = false;

            using (ExtWebClient wc = new ExtWebClient())
            {
                wc.Encoding = Encoding.UTF8;
                if (!string.IsNullOrEmpty(txtUserAgent.Text))
                {
                    wc.Headers.Add(HttpRequestHeader.UserAgent, txtUserAgent.Text);
                }
                else
                {
                    wc.Headers.Add(HttpRequestHeader.UserAgent, "ExtWebClient");
                }
                wc.UploadMultiFilesCompleted += new EventHandler <UploadMultiFilesCompletedEventArgs>(delegate(object s, UploadMultiFilesCompletedEventArgs se)
                {
                    if (se.Result == UploadMultiFilesCompletedResult.Failed)
                    {
                        WriteLog(Color.Red, "# 發生錯誤, Status Code:{0}/{1}", (int)se.StatusCode, se.StatusCode);
                        if (se.Response != null && se.Response.Length != 0)
                        {
                            WriteLog(Color.Red, Encoding.UTF8.GetString(se.Response));
                        }
                    }
                    else
                    {
                        WriteLog(Color.Green, "# 上傳完成");
                        WriteLog(Color.Green, "# 自伺服器收到:{0}", Encoding.UTF8.GetString(se.Response));
                    }
                    done = true;
                });
                wc.UploadMultiFilesProgressChanged += new EventHandler <UploadMultiFilesProgressChangedEventArgs>(delegate(object s, UploadMultiFilesProgressChangedEventArgs se)
                {
                    this.Invoke(new MethodInvoker(() => { pbPercentage.Value = se.ProgressPercentage; Application.DoEvents(); }));
                });
                NameValueCollection          nvc   = GetNameValueCollection();
                List <ExtWebClient.FileData> files = new List <ExtWebClient.FileData>();
                CJF.Utility.CRC.Crc16        crc   = new CJF.Utility.CRC.Crc16();
                if (!string.IsNullOrEmpty(txtFile1.Text) && File.Exists(txtFile1.Text))
                {
                    files.Add(new ExtWebClient.FileData()
                    {
                        ContentType = ConvUtils.GetContentType(txtFile1.Text),
                        FileName    = txtFile1.Text,
                        KeyName     = "File1"
                    });
                    nvc.Add("File1CRC", crc.ComputeHash(File.ReadAllBytes(txtFile1.Text)).ToHexString(""));
                }
                if (!string.IsNullOrEmpty(txtFile2.Text) && File.Exists(txtFile2.Text))
                {
                    files.Add(new ExtWebClient.FileData()
                    {
                        ContentType = ConvUtils.GetContentType(txtFile2.Text),
                        FileName    = txtFile2.Text,
                        KeyName     = "File2"
                    });
                    nvc.Add("File2CRC", crc.ComputeHash(File.ReadAllBytes(txtFile2.Text)).ToHexString(""));
                }
                wc.UploadMultiFilesAsync(uri, nvc, files.ToArray(), null);
                DateTime now = DateTime.Now;
                while (!done && DateTime.Now.Subtract(now).TotalSeconds <= 60)
                {
                    Application.DoEvents();
                }
            }
        }