An implementation of C # upload file

Source: Internet
Author: User
Upload
An implementation of C # upload file
Using System;
Using System.Data;
Using System.Data.SqlClient;
Using Ca.components; All under the component namespace

Namespace Ca.components
{
<summary>
General's summary description.
Release Date: 2002-8-8 Original author: Thor
This program belongs to the mode group HTTP://WWW.AI361.COM/PROJECT/
Testing through in SQL2000,WIN2000S+.NET+IIS5
</summary>
public class General
{
private string FilePath; File path

Defines the information that an enumeration uses to hold a file
public enum File
{
File_size,//Size
File_postname,//
File_sysname,
File_orginname,
File_path
};
Constructors
Public General ()
{
Set the appsettings["filepath" in Web.config to store the path to the file.
FilePath = system.configuration.configurationsettings.appsettings["FilePath"];
}

<summary>
Upload file Common function, this method is static, the system can be called at any time.
</summary>
<param name= "File" > parameter is the file control object for the page </param>
<returns></returns>
public static string[] UploadFile (htmlinputfile file)
{
string[] arr = new STRING[5];
Through the system time to generate file name, this feature can be closed, but the Chinese long file name support 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 are have any question or comments,please mail to me (lsmodel@ai361.com) or discuss in our forum (http://www.ai361.com/ BBS).
finished text


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.