/// <Summary>
/// WebClient uploads a file to the server
/// </Summary>
/// <Param name = "localfilepath"> file name, full path format </param>
/// <Param name = "serverfolder"> server folder path </param>
/// <Returns> </returns>
Public bool upload (string localfilepath, out string Foldername, string newfilename)
{
// Create a folder first
Foldername = "";
Try
{
Guid = guid. newguid ();
Foldername = guid. tostring ();
String diskpath = Dal. databaseoperator. getvaluefromapplictionconfig ("diskpath ");
If (! Diskpath. endswith ("/")&&! Diskpath. endswith (@"\"))
{
Diskpath = diskpath + "/";
}
Diskpath + = Foldername;
If (! Directory. exists (diskpath ))
{
// Create a folder on the server
Directory. createdirectory (diskpath );
}
// Upload data again
String serverfolder = Dal. databaseoperator. getvaluefromapplictionconfig ("uploadpath ");
If (! Serverfolder. endswith ("/")&&! Serverfolder. endswith (@"\"))
{
Serverfolder = serverfolder + "/";
}
String uristring = serverfolder + Foldername + "/" + newfilename;
/// Create a WebClient instance
WebClient mywebclient = new WebClient ();
Mywebclient. Credentials = credentialcache. defaultcredentials;
// File to be uploaded
Filestream FS = new filestream (newfilename, filemode. Open, fileaccess. Read );
// Determine the file size
String strfilesize = Dal. databaseoperator. getvaluefromapplictionconfig ("filesize ");
Int filesize = convert. toint32 (strfilesize) * 1024*1024;
If (FS. length> filesize)
{
MessageBox. Show ("the attachment you uploaded cannot exceed" + strfilesize + "M ");
Return false;
}
Binaryreader r = new binaryreader (FS );
// The uploadfile method can be used in the following format
Mywebclient. uploadfile (uristring, "put", localfilepath );
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! ");
}
Application. doevents ();
Poststream. Close ();
}
Catch (exception ERR)
{
// MessageBox. Show ("File Upload Failed. Please try again later ~ ");
Dal. log. filelogsys. filelog. writelog (ERR. Message + err. stacktrace );
Return false;
}
Return true;
}