c# - Pass parameters to WebClient.DownloadFileCompleted event -
i using webclient.downloadfileasync()
method, , wanted know how can pass parameter webclient.downloadfilecompleted
event (or other event matter), , use in invoked method.
my code:
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 use filenameid's value here? } }
how can pass parameter dosomethingonfinish()
?
you can use webclient.querystring.add("filename", yourfilenameid);
add information.
then access in dosomethingonfinish
function,
use string myfilenameid = ((system.net.webclient)(sender)).querystring["filename"];
receive file name.
this code should like:
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 use filenameid's value here? string myfilenameid = ((system.net.webclient)(sender)).querystring["filename"]; }
even if should work, should using unity's unitywebrequest
class. haven't heard should like:
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 binary data byte[] results = www.downloadhandler.data; } }
Comments
Post a Comment