// Use the following method to upload files to any directory on the server
# Region private method save the file to the server SaveFile (system. Web. UI. webcontrols. fileupload, string savetourl)
/// <Summary>
/// Save the file to the server
/// </Summary>
/// <Param name = "fileupload"> upload Control </param>
/// <Param name = "savetourl"> Save path </param>
/// <Returns> check whether the file is successfully saved </returns>
Private bool SaveFile (system. Web. UI. webcontrols. fileupload, string savetourl)
{
Try
{
Filestream FS = new filestream (savetourl, filemode. openorcreate, fileaccess. Write );
FS. Write (this. getbyte (fileupload), 0, fileupload. postedfile. contentlength );
FS. Close ();
Return true;
}
Catch
{
Return false;
}
}
# Endregion
# Region private method to obtain the byte array getbyte (system. Web. UI. webcontrols. fileupload)
/// <Summary>
/// Obtain the byte array of the uploaded file
/// </Summary>
/// <Param name = "fileupload"> upload Control </param>
/// <Returns> Returns a byte array </returns>
Private byte [] getbyte (system. Web. UI. webcontrols. fileupload)
{// Obtain the converted byte array
// Get the file name to be uploaded
String strfilepathname = fileupload. postedfile. filename;
String strfilename = path. getfilename (strfilepathname );
Int filelength = fileupload. postedfile. contentlength;
// Upload a file
Byte [] filebytearray = new byte [filelength]; // temporary storage of byte Arrays for image files
Stream streamobject = fileupload. postedfile. inputstream; // create a data stream object
// Read image file data. filebytearray is the data storage body, 0 is the Data Pointer position, and filelne is the data length.
Streamobject. Read (filebytearray, 0, filelength );
Return filebytearray;
}
# Endregion