将参数传递给 WebClient.DownloadFileCompleted 事件
Pass parameters to WebClient.DownloadFileCompleted event
我正在使用 WebClient.DownloadFileAsync()
方法,想知道如何将参数传递给 WebClient.DownloadFileCompleted
事件(或与此相关的任何其他事件),并在调用的事件中使用它方法。
我的代码:
public class MyClass
{
string downloadPath = "some_path";
void DownloadFile()
{
int fileNameID = 10;
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += DoSomethingOnFinish;
Uri uri = new Uri(downloadPath + "\" + fileNameID );
webClient.DownloadFileAsync(uri,ApplicationSettings.GetBaseFilesPath +"\" + fileNameID);
}
void DoSomethingOnFinish(object sender, AsyncCompletedEventArgs e)
{
//How can i use fileNameID's value here?
}
}
如何将参数传递给 DoSomethingOnFinish()
?
您可以使用 webClient.QueryString.Add("FileName", YourFileNameID);
添加额外的信息。
然后在您的 DoSomethingOnFinish
函数中访问它,
使用string myFileNameID = ((System.Net.WebClient)(sender)).QueryString["FileName"];
接收文件名。
代码应该是这样的:
string downloadPath = "some_path";
void DownloadFile()
{
int fileNameID = 10;
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DoSomethingOnFinish);
webClient.QueryString.Add("fileName", fileNameID.ToString());
Uri uri = new Uri(downloadPath + "\" + fileNameID);
webClient.DownloadFileAsync(uri,ApplicationSettings.GetBaseFilesPath +"\" + fileNameID);
}
void DoSomethingOnFinish(object sender, AsyncCompletedEventArgs e)
{
//How can i use fileNameID's value here?
string myFileNameID = ((System.Net.WebClient)(sender)).QueryString["fileName"];
}
即使这可行,您也应该使用 Unity 的 UnityWebRequest
class。你可能没有听说过它,但它应该是这样的:
void DownloadFile(string url)
{
StartCoroutine(downloadFileCOR(url));
}
IEnumerator downloadFileCOR(string url)
{
UnityWebRequest www = UnityWebRequest.Get(url);
yield return www.Send();
if (www.isError)
{
Debug.Log(www.error);
}
else
{
Debug.Log("File Downloaded: " + www.downloadHandler.text);
// Or retrieve results as binary data
byte[] results = www.downloadHandler.data;
}
}
我正在使用 WebClient.DownloadFileAsync()
方法,想知道如何将参数传递给 WebClient.DownloadFileCompleted
事件(或与此相关的任何其他事件),并在调用的事件中使用它方法。
我的代码:
public class MyClass
{
string downloadPath = "some_path";
void DownloadFile()
{
int fileNameID = 10;
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += DoSomethingOnFinish;
Uri uri = new Uri(downloadPath + "\" + fileNameID );
webClient.DownloadFileAsync(uri,ApplicationSettings.GetBaseFilesPath +"\" + fileNameID);
}
void DoSomethingOnFinish(object sender, AsyncCompletedEventArgs e)
{
//How can i use fileNameID's value here?
}
}
如何将参数传递给 DoSomethingOnFinish()
?
您可以使用 webClient.QueryString.Add("FileName", YourFileNameID);
添加额外的信息。
然后在您的 DoSomethingOnFinish
函数中访问它,
使用string myFileNameID = ((System.Net.WebClient)(sender)).QueryString["FileName"];
接收文件名。
代码应该是这样的:
string downloadPath = "some_path";
void DownloadFile()
{
int fileNameID = 10;
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DoSomethingOnFinish);
webClient.QueryString.Add("fileName", fileNameID.ToString());
Uri uri = new Uri(downloadPath + "\" + fileNameID);
webClient.DownloadFileAsync(uri,ApplicationSettings.GetBaseFilesPath +"\" + fileNameID);
}
void DoSomethingOnFinish(object sender, AsyncCompletedEventArgs e)
{
//How can i use fileNameID's value here?
string myFileNameID = ((System.Net.WebClient)(sender)).QueryString["fileName"];
}
即使这可行,您也应该使用 Unity 的 UnityWebRequest
class。你可能没有听说过它,但它应该是这样的:
void DownloadFile(string url)
{
StartCoroutine(downloadFileCOR(url));
}
IEnumerator downloadFileCOR(string url)
{
UnityWebRequest www = UnityWebRequest.Get(url);
yield return www.Send();
if (www.isError)
{
Debug.Log(www.error);
}
else
{
Debug.Log("File Downloaded: " + www.downloadHandler.text);
// Or retrieve results as binary data
byte[] results = www.downloadHandler.data;
}
}