本文整理汇总了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");
}
}
示例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 ();
}
示例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;
}
示例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;
}
示例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";
}
}