C # An Implementation of File Upload

Source: Internet
Author: User

Using system;
Using system. Data;
Using system. Data. sqlclient;
Using Ca. components; // all are in the component namespace

Namespace ca. Components
{
/// <Summary>
/// Summary of General.
/// Release Date: Author: Raytheon
/// ThisProgramMode group http: // www. ai361.com/project/
/// Passed the test in SQL2000, win2000s +. NET + iis5
/// </Summary>
Public class General
{
Private string filepath; // file path

// Define an enumeration to store file information
Public Enum File
{
File_size, // size
File_postname ,//
File_sysname,
File_orginname,
File_path
};
// Constructor
Public General ()
{
// Set the path for storing files in Web. config ["filepath.
Filepath = system. configuration. configurationsettings. deleettings ["filepath"];
}

/// <Summary>
/// General function for uploading files. This method is static and can be called by the system at any time.
/// </Summary>
/// <Param name = "file"> the parameter is the file control object on the page. </param>
/// <Returns> </returns>
Public static string [] uploadfile (htmlinputfile file)
{
String [] arr = new string [5];
// Generate a file name through the system time. This function can be closed, but the support for long Chinese file names is not good.
String filename = datetime. Now. tostring (). Replace ("", ""). Replace (":", ""). Replace ("-","");
String fileorginname = file. postedfile. filename. substring (file. postedfile. filename. lastindexof ("\") + 1 );
If (file. postedfile. contentlength <= 0)
Return NULL;
String postfilename;
String Path = new general (). filepath + "\\";
Try
{
Int Pos = file. postedfile. filename. lastindexof (".") + 1;
Postfilename = file. postedfile. filename. substring (Pos, file. postedfile. filename. Length-Pos );
File. postedfile. saveas (path + filename + "." + postfilename );
}
Catch (exception Exec)
{
Throw (EXEC );
}
Double unit = 1024;
Double Size = math. Round (file. postedfile. contentlength/unit, 2 );
Arr [(INT) file. file_size] = size. tostring (); // File Size
Arr [(INT) file. file_postname] = postfilename; // file type (File suffix)
Arr [(INT) file. file_sysname] = filename; // file system name
Arr [(INT) file. file_orginname] = fileorginname; // original file name
Arr [(INT) file. file_path] = path + filename + "." + postfilename; // file path
Return arr;
// Throw (new exception (htmlutility. htmlencode (IDNO. postedfile. filename )));
}
}
}

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.