1. File Upload
/// <Summary>
/// WebClient uploads a file to the server
/// </Summary>
/// <Param name = "filenamepath"> file name, full path format </param>
/// <Param name = "uristring"> server folder path </param>
Private void uploadfile (string filenamepath, string uristring)
{
String filename = filenamepath. substring (filenamepath. lastindexof ("\") + 1 );
Newfilename = datetime. Now. tostring ("yymmddhhmmss") + datetime. Now. millisecond. tostring () + filenamepath. substring (filenamepath. lastindexof ("."));
String filenameext = filename. substring (filename. lastindexof (".") + 1 );
If (uristring. endswith ("/") = false) uristring = uristring + "/";
Uristring = uristring + newfilename;
Upfilename = newfilename;
/**/
/// Create a WebClient instance
WebClient mywebclient = new WebClient ();
Mywebclient. Credentials = credentialcache. defaultcredentials;
// File to be uploaded
Filestream FS = new filestream (filenamepath, filemode. Open, fileaccess. Read );
// Filestream FS = openfile ();
Binaryreader r = new binaryreader (FS );
Try
{
// The uploadfile method can be used in the following format
// Mywebclient. uploadfile (uristring, "put", filenamepath );
Byte [] postarray = R. readbytes (INT) fs. Length );
Stream poststream = mywebclient. openwrite (uristring, "put ");
If (poststream. canwrite)
{
Poststream. Write (postarray, 0, postarray. Length );
}
Else
{
MessageBox. Show ("file cannot be written currently! ");
}
Poststream. Close ();
}
Catch
{
MessageBox. Show ("File Upload Failed. Please try again later ~ ");
}
}
2. File Download
String filepath = system. appdomain. currentdomain. basedirectory. tostring () + "programout. xml"; // path
WebClient client = new WebClient ();
String filename = filepath. substring (filepath. lastindexof ("\") + 1); // The downloaded file name
Savefiledialog = new savefiledialog ();
Savefiledialog. Filter = "XML file | *. xml ";
Savefiledialog. filterindex = 2;
Savefiledialog. restoredirectory = true;
If (savefiledialog. showdialog () = dialogresult. OK)
{
String fname = savefiledialog. filename;
String Path = fname; // save as absolute path + file name
Try
{
Webrequest myre = webrequest. Create (PATH );
}
Catch
{
MessageBox. Show ("File Download failed", "error ");
}
Try
{
Client. downloadfile (filepath, PATH );
MessageBox. Show ("file exported successfully !! "," System prompt! ", Messageboxbuttons. OK, messageboxicon. information );
}
Catch
{
MessageBox. Show ("File Download failed", "error ");
}