Add a namespace:
Using system. net;
Using system. IO;
/// <Summary>
/// Download an object
/// </Summary>
/// <Param name = "url"> URL of the file to be downloaded </param>
Public void downloadfile (string URL)
{
WebClient client = new WebClient ();
Int n = URL. lastindexof ('/');
String urladdress = URL. substring (0, n); // obtain the URL
String filename = URL. substring (n + 1, URL. Length-n-1); // get the file name
String dir = server. mappath ("./"); // download file storage path
String Path = dir + '\' + filename; // complete path for storing the downloaded file
Stream stream = client. openread (URL );
Streamreader reader = new streamreader (Stream );
Byte [] Mbyte = new byte [100000];
Int allbyte = (INT) Mbyte. length;
Int startbyte = 0;
While (allbyte> 0) // read cyclically
{
Int M = stream. Read (Mbyte, startbyte, allbyte );
If (M = 0)
Break;
Startbyte + = m;
Allbyte-= m;
}
Filestream fstr = new filestream (path, filemode. openorcreate, fileaccess. Write );
Fstr. Write (Mbyte, 0, startbyte); // write the file
Stream. Close ();
Fstr. Close ();
}
Test passed