Winform uploads files to the server

Source: Internet
Author: User

/// <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;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.