C # Upload a file

xiaoxiao2021-03-06  73

2004-05-18 LSMODEL AI361.COM

C # Upload a file

Using system;

Using system.data;

Using system.data.sqlclient;

Using ca.components; // All in the component namespace

Namespace Ca.components

{

///

/// General's summary description.

/// Date: 2002-8-8 author: Thor

/// This procedure mode group http://www.ai361.com/project/

/// Test in SQL2000, Win2000S .NET IIS5

///

Public class general

{

Private string filepath; // file path

/ / Define an enumeration to store information

Public enum file

{

FILE_SIZE, / / ​​Size

FILE_POSTNAME, //

File_sysname,

FILE_ORGINNAME,

FILE_PATH

}

//Constructor

Public general ()

{

// Set AppSettings ["FilePath"] in Web.config, used to store the path of the file.

Filepath = system.configuration.configurationSettings.appsettings ["filepath"];

}

///

/// Upload the file universal function, this method is static, and the system can be called.

///

/// parameter is the File control object of the page

///

Public static string [] UPLOADFILE (HTMLINPUTFILE FILE)

{

String [] arr = new string [5];

// Generate file names through the system time, this feature can be closed, but the Chinese long file name 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 name)

Arr [(int) file.file_sysName] = filename; // file system name

Arr [(int) file.file_orginname] = fileorginname; // file original name

Arr [(int) file.file_path] = path filename "." postfilename; // file path

Return ARR;

// throw (new exception (htmlutility.htmlencode (idno.postedfile.filename));

}

}

}

If you have any questions or comments, please mail to me (lsmodel@ai361.com) or discuss in ouirt (http://www.ai361.com/bbs).

Finish

转载请注明原文地址:https://www.9cbs.com/read-120152.html

New Post(0)