当前位置: 首页>>代码示例>>C#>>正文


C# WebClient.UploadFileTaskAsync方法代码示例

本文整理汇总了C#中System.Net.WebClient.UploadFileTaskAsync方法的典型用法代码示例。如果您正苦于以下问题:C# WebClient.UploadFileTaskAsync方法的具体用法?C# WebClient.UploadFileTaskAsync怎么用?C# WebClient.UploadFileTaskAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.WebClient的用法示例。


在下文中一共展示了WebClient.UploadFileTaskAsync方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: PostRequestFile

 private async static Task<byte[]> PostRequestFile(string url)
 {
     using (var client = new WebClient())
     {
         client.Headers.Add("user-agent", "my own code");
         client.Headers.Add("content-type", "x-www-form-urlencoded");
         client.UploadProgressChanged += client_UploadProgressChanged;
         return await client.UploadFileTaskAsync(url, "asdf");
     }
 }
开发者ID:shayim,项目名称:Programming-DotNet,代码行数:10,代码来源:Program.cs

示例2: UploadFileAsyncContentType

		[Category ("AndroidNotWorking")] // Test suite hangs if the tests runs as part of the entire BCL suite. Works when only this fixture is ran
		public void UploadFileAsyncContentType ()
		{
			var serverUri = "http://localhost:13370/";
			var filename = Path.GetTempFileName ();

			HttpListener listener = new HttpListener ();
			listener.Prefixes.Add (serverUri);
			listener.Start ();

			using (var client = new WebClient ())
			{
				client.UploadFileTaskAsync (new Uri (serverUri), filename);
				var request = listener.GetContext ().Request;

				var expected = "multipart/form-data; boundary=---------------------";
				Assert.AreEqual (expected.Length + 15, request.ContentType.Length);
				Assert.AreEqual (expected, request.ContentType.Substring (0, expected.Length));
			}
			listener.Close ();
		}
开发者ID:fabriciomurta,项目名称:mono,代码行数:21,代码来源:WebClientTest.cs

示例3: UploadFile

 private static async Task<string> UploadFile(string url, string fName)
 {
     WebClient wClient = new WebClient();
     wClient.UploadProgressChanged += wClient_UploadProgressChanged;   
     var ans = await wClient.UploadFileTaskAsync(url, "POST", fName);
     string res = System.Text.Encoding.Default.GetString(ans);
     return res;
 }
开发者ID:alikhil,项目名称:vk-photo-uploader,代码行数:8,代码来源:VKPhotoUploader.cs

示例4: UploadImage

 private async Task<byte[]> UploadImage(string url, string filePath, NameValueCollection valueCollection = null)
 {
     using (var webClient = new WebClient())
     {
         try
         {
             webClient.UploadProgressChanged += WebClientUploadProgressChanged;
             webClient.UploadFileCompleted += WebClientUploadCompleted;
             if (valueCollection != null) webClient.Headers.Add(valueCollection);
             if (File.Exists(filePath))
             {
                 string fileType = Path.GetExtension(filePath).Remove(0, 1).ToLower();
                 if (fileType == "jpg") fileType = "jpeg";
                 webClient.Headers.Add(HttpRequestHeader.ContentType, "image/" + fileType);
                 return await webClient.UploadFileTaskAsync(new Uri(url), filePath);
             }
         }
         catch (Exception ex)
         {
             Log.Add(ex);
             MessageBox.Show(ex.Message, Properties.Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error);
         }
     }
     return null;
 }
开发者ID:gmikhail,项目名称:gameru-images-uploader,代码行数:25,代码来源:MainWindow.xaml.cs

示例5: MyMainAsync

 //       public async Task<string> MyMainAsync(int i, string h, string u, string p, string f, CancellationToken cancel, IProgress<string> progress)
        public async Task<string> MyMainAsync(int i, FTPConfiguration ftpconfiguration, CancellationToken cancel, IProgress<string> progress)
        {
            using (WebClient client = new WebClient())
            {
                try
                {
                    //--- Record the start time---
                    startTime = DateTime.Now;
                    // First create a Stopwatch instance
                    stopwatch = new Stopwatch();
                    ctime1 = ctime2 = ctime3 = ctime4 = ctime5 = ptime1 = ptime2 = ptime3 = ptime4 = ptime5 = 0;    // zero out each run
                    // Begin the stopwatch
                    stopwatch.Start();
                    cancel.ThrowIfCancellationRequested();
                    if (cancel.IsCancellationRequested)
                    {
                        client.CancelAsync();
                    }
                    // Get the object used to communicate with the server.
                    //                WebClient client = new WebClient();
                    client.Proxy = null;
                    client.BaseAddress = "ftp://" + ftpconfiguration.Host;
// TODO: put back in /demo/ when doing hardwired for real
//                    client.BaseAddress = "ftp://" + ftpconfiguration.Host + "/demo/";     

                    CredentialCache cache = new CredentialCache();
                    NetworkCredential credential = new NetworkCredential(ftpconfiguration.Username, ftpconfiguration.Password);

                    client.Credentials = credential;

                    if (i == 0)
                    { client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressChanged1); }
                    else if (i == 1)
                    { client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressChanged2); }
                    else if (i == 2)
                    { client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressChanged3); }
                    else if (i == 3)
                    { client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressChanged4); }
                    else if (i == 4)
                    { client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressChanged5); }

                    //client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressChanged); 
                    //client.UploadFileCompleted += new UploadFileCompletedEventHandler(client_UploadFileCompleted);
                    Uri remoteFile;
                    Uri.TryCreate(ftpconfiguration.Filename, System.UriKind.Relative, out remoteFile);

                    FileInfo fi = new FileInfo(@demoDir + ftpconfiguration.Filename);
                    //fileSize = fi.Length;
                    //                await client.DownloadFileTaskAsync(remoteFile, "/temp/demo/RRVideo.mp4");

                    Task uploadTask = client.UploadFileTaskAsync(remoteFile, @demoDir + ftpconfiguration.Filename);
                    //string sNull = null;
                    //client.UploadFileAsync(remoteFile, sNull, @demoDir + f, i.ToString());
                    //client.UploadFile(remoteFile, @demoDir + f);

                    //if (i == 3)
                    //{
                    //   await MyDelayAsync(i);
                    //}
                    await uploadTask;
                    ////update the progress on UI
//                  label1.Invoke((Action)delegate { ReportProgress2(1, i, Convert.ToString(fi.Length) + " bytes sent", 0); });

                    if (i == 0)     // stop Elapsed timer when last Task done.
                    {
                        label1.Invoke((Action)delegate { timer1.Change(Timeout.Infinite, Timeout.Infinite); });
                    }


                    if (cancel.IsCancellationRequested)
                    {
                        client.CancelAsync();
                    }
                    //showTime();

                }
                catch (OperationCanceledException oce)
                {
                    client.CancelAsync();
                    //label1.Text = "Cancelled";
                }
                catch (Exception e)
                {
                    ReportError(e.Message, i);
                }
                return "end";
            }
        }
开发者ID:MatthewWelch,项目名称:FTPDemo,代码行数:89,代码来源:FTPDemoForm1.cs


注:本文中的System.Net.WebClient.UploadFileTaskAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。