Upload button for self-written files

Source: Internet
Author: User

Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Web;
Using System. Web. SessionState;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. HtmlControls;
Using System. IO;
Using System. Text;
Using levin;

Namespace levin. Files
{
/// <Summary>
/// Summary of UpDown.
/// </Summary>
Public class UpDown
{
Public UpDown ()
{
//
// TODO: add the constructor logic here
//
}
Public static bool IsAllowedExtension (HtmlInputFile hifile)
{
String strOldFilePath = "", strExtension = "";

// Indicates the extension that can be uploaded. It can be read from the configuration file.
String [] arrExtension = {". gif", ". jpg", ". jpeg", ". bmp", ". png "};

If (hifile. PostedFile. FileName! = String. Empty)
{
StrOldFilePath = hifile. PostedFile. FileName;
// Get the extension of the uploaded file
StrExtension = strOldFilePath. Substring (strOldFilePath. LastIndexOf ("."));
// Determine whether the extension is valid
For (int I = 0; I <arrExtension. Length; I ++)
{
If (strExtension. Equals (arrExtension [I])
{
Return true;
}
}
}
Return false;
}
Public static bool isallowedextension (string filetype)
{
// String stroldfilepath = ""
// String strextension = "";

// Indicates the extension that can be uploaded. It can be read from the configuration file.
String [] arrextension = {". GIF", ". jpg", ". jpeg", ". BMP", ". PNG "};

If (filetype! = String. Empty)
{
// Determine whether the extension is valid
For (INT I = 0; I <arrextension. length; I ++)
{
If (filetype. Equals (arrextension [I])
{
Return true;
}
}
}
Return false;
}

Public static bool isallowedlength (htmlinputfile hifile)
{
// Maximum size of the file that can be uploaded. It can be stored in an XML file, measured in KB.
Int I = 20;
// If the size of the uploaded file exceeds the maximum value, flase is returned; otherwise, true is returned.
If (hifile. postedfile. contentlength> I * 1024)
{
Return false;
}
Return true;
}
Public static bool IsAllowedLength (string filesize)
{
// Maximum size of the file that can be uploaded. It can be stored in an xml file, measured in KB.
Int I = 20;
// If the size of the uploaded file exceeds the maximum value, flase is returned; otherwise, true is returned.
If (int. Parse (filesize)> I * 1024)
{
Return false;
}
Return true;
}
Public static bool IsAllowedLength (int filesize)
{
// Maximum size of the file that can be uploaded. It can be stored in an xml file, measured in KB.
Int I = 20;
// If the size of the uploaded file exceeds the maximum value, flase is returned; otherwise, true is returned.
If (filesize> I * 1024)
{
Return false;
}
Return true;
}

Public static FileUpModem SaveFile (HtmlInputFile fileUp, string strAbsolutePath)
{
FileUpModem mod = new FileUpModem ();
String strOldFilePath = "", strExtension = "", strNewFileName = "", mFileName = "";

If (fileUp. PostedFile. FileName! = String. Empty)
{
StrOldFilePath = fileUp. PostedFile. FileName;
// Get the extension of the uploaded file
Strextension = stroldfilepath. substring (stroldfilepath. lastindexof ("."));
// Name of the uploaded file
Strnewfilename = util. apitime. datetimeformat () + strextension;
Mfilename = stroldfilepath. substring (stroldfilepath. lastindexof ("//") + 1); // get the file name

Mod. filetype = stroldfilepath. substring (stroldfilepath. lastindexof ("."));
Mod. size = convert. tostring (fileup. postedfile. contentlength );
Mod. Name = stroldfilepath. substring (0, mfilename. lastindexof (".") + util. apitime. datetimeformat () + mod. filetype;

If (strabsolutepath. lastindexof ("//") = strabsolutepath. length)
{
Fileup. postedfile. saveas (strabsolutepath + strnewfilename );
}
Else
{
Fileup. postedfile. saveas (strabsolutepath + "//" + strnewfilename );
}
}
Return MOD;
}

Public static fileupmodem uploadfile (system. Web. UI. htmlcontrols. htmlinputfile fileup, string path)
{
Fileupmodem mod = new fileupmodem ();
String filepath = "", fileextname = ", mfilename, mpath, Spath;

If (""! = Fileup. postedfile. filename)
{
FilePath = fileUp. PostedFile. FileName; // obtain the file path
FileExtName = filePath. Substring (filePath. LastIndexOf (".") + 1 );
Try
{
// Obtain the physical file path corresponding to the specified virtual path on the Web server.
SPath = HttpContext. Current. Server. MapPath ("upload /");
MPath = (path = "0 ")? SPath: sPath + path + @"/";
MFileName = filePath. Substring (filePath. LastIndexOf ("//") + 1); // get the file name
// Set the file Model
Mod. FileType = mFileName. Substring (mFileName. LastIndexOf ("."));
Mod. Size = Convert. ToString (fileUp. PostedFile. ContentLength );
Mod. Name = mFileName. Substring (0, mFileName. LastIndexOf (".") + util. ApiTime. DateTimeFormat () + mod. FileType;
// Save the uploaded file to the specified directory
// Levin. api. WebResponse. alert (mPath + mFileName );
If (! IsAllowedExtension (mod. FileType) api. WebResponse. alertGoBack (Const. Mess_FileTypeNoAllow );
If (! IsAllowedLength (mod. Size) api. WebResponse. alertGoBack (Const. Mess_FileLengthNoAllow );

FileUp. PostedFile. SaveAs (mPath + mod. Name );
// Set the file Model
Mod. PrevName = mFileName;
Mod. absolutepath = "/upload/" + path + @ "/" + mod. Name;
Mod. currentpath = mpath + mfilename;
Mod. httppath = files. fileapi. abspath (mod. Name );
// Httpcontext. Current. response. Write (Levin );
Httpcontext. Current. response. Write (mod. absolutepath );
Levin. API. webresponse. Alert (Const. mess_uploadsuccess );
}
Catch
{
Levin. API. webresponse. Alert (Const. mess_uploadfalse );
}
}
Return MOD;
}
}
Public class FileUpModem
{
Private string strSize;
Private string strName;
Private string strPrevName;
Private string strAbsolutePath;
Private string strCurrentPath;
Private string strFileType;
Private string strHttpPath;

Public string HttpPath
{
Get {return strHttpPath ;}
Set {strHttpPath = value ;}
}
Public string FileType
{
Get {return strFileType ;}
Set {strFileType = value ;}
}
Public String size
{
Get {return strsize ;}
Set {strsize = value ;}
}
Public string name
{
Get {return strname ;}
Set {strname = value ;}
}
Public String prevname
{
Get {return strprevname ;}
Set {strprevname = value ;}
}
/// <Summary>
/// Relative path
/// </Summary>
Public String absolutepath
{
Get {return strabsolutepath ;}
Set {strabsolutepath = value ;}
}
Public String currentpath
{
Get {return strcurrentpath ;}
Set {strcurrentpath = value ;}
}

}
}

 

Demo

<% @ Page Language = "C #" codebehind = "webform1.aspx. cs" autoeventwireup = "false" inherits = "Levin. webform1" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> webform1 </title>
<Meta name = "generator" content = "Microsoft Visual Studio. NET 7.1">
<Meta name = "CODE_LANGUAGE" Content = "C #">
<Meta name = "vs_defaultClientScript" content = "JavaScript">
<Meta name = "vs_targetSchema" content = "http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<Body MS_POSITIONING = "GridLayout">
<Form id = "Form1" method = "post" runat = "server">
<Asp: Button id = "Button1" style = "Z-INDEX: 101; LEFT: 280px; POSITION: absolute; TOP: 248px" runat = "server"
Text = "Button"> </asp: Button>
<Asp: Button id = "Button2" style = "Z-INDEX: 102; LEFT: 336px; POSITION: absolute; TOP: Running PX" runat = "server"
Text = "Send email"> </asp: Button>
<INPUT id = "FileUp" runat = "server" style = "Z-INDEX: 103; LEFT: 336px; POSITION: absolute; TOP: 56px"
Type = "file">
<Asp: Button id = "Button3" style = "Z-INDEX: 104; LEFT: 576px; POSITION: absolute; TOP: 56px" runat = "server"
Text = "Button"> </asp: Button>
<Asp: Button id = "Button4" style = "Z-INDEX: 105; LEFT: 656px; POSITION: absolute; TOP: 48px" runat = "server"
Text = "read file"> </asp: Button>
<Asp: TextBox id = "TextBox1" style = "Z-INDEX: 106; LEFT: 352px; POSITION: absolute; TOP: 168px"
Runat = "server" Width = "168px" Height = "96px" TextMode = "MultiLine"> </asp: TextBox>
<Asp: Button id = "Button5" style = "Z-INDEX: 107; LEFT: 600px; POSITION: absolute; TOP: 232px" runat = "server"
Text = "write file"> </asp: Button>
<Asp: Button id = "Button6" style = "Z-INDEX: 108; LEFT: 584px; POSITION: absolute; TOP: 160px" runat = "server"
Text = "append file"> </asp: Button>
<Asp: Button id = "Button7" style = "Z-INDEX: 109; LEFT: pixel PX; POSITION: absolute; TOP: 320px" runat = "server"
Text = "read liu file"> </asp: Button>
<Asp: Button id = "Button8" style = "Z-INDEX: 110; LEFT: 312px; POSITION: absolute; TOP: Running PX" runat = "server"
Text = "cn rili"> </asp: Button>
<Asp: Button id = "Button9" style = "Z-INDEX: 111; LEFT: 520px; POSITION: absolute; TOP: Running PX" runat = "server"
Text = "Read File"> </asp: Button>
</Form>
</Body>
</HTML>

 

Cs

Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Web;
Using System. Web. SessionState;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. HtmlControls;
Using levin;
Namespace levin
{
/// <Summary>
/// Summary of WebForm1.
/// </Summary>
Public class WebForm1: System. Web. UI. Page
{
Protected System. Web. UI. WebControls. Button Button2;
Protected System. Web. UI. WebControls. Button Button3;
Protected System. Web. UI. WebControls. Button Button1;
Protected System. Web. UI. WebControls. Button Button4;
Protected System. Web. UI. WebControls. TextBox TextBox1;
Protected System. Web. UI. WebControls. Button Button5;
Protected System. Web. UI. WebControls. Button Button6;
Protected System. Web. UI. WebControls. Button Button7;
Protected System. Web. UI. WebControls. Button Button8;
Protected System. Web. UI. WebControls. Button Button9;
Protected System. Web. UI. HtmlControls. HtmlInputFile FileUp;


 
Private void Page_Load (object sender, System. EventArgs e)
{
// Place user code here to initialize the page
}

# Code generated by region Web Form Designer
Override protected void OnInit (EventArgs e)
{
//
// CODEGEN: This call is required by the ASP. NET Web form designer.
//
InitializeComponent ();
Base. OnInit (e );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. Button1.Click + = new System. EventHandler (this. button#click );
This. Button2.Click + = new System. EventHandler (this. Button2_Click );
This. Button3.Click + = new System. EventHandler (this. Button3_Click );
This. Button4.Click + = new System. EventHandler (this. Button4_Click );
This. Button5.Click + = new System. EventHandler (this. Button5_Click );
This. Button6.Click + = new System. EventHandler (this. Button6_Click );
This. Button7.Click + = new System. EventHandler (this. Button7_Click );
This. button8.click + = new system. eventhandler (this. button8_click );
This. button9.click + = new system. eventhandler (this. button9_click );
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion

Private void button#click (Object sender, system. eventargs E)
{
Levin. API. webresponse. Alert ("AAA", "webform2.aspx ");
}

Private void button2_click (Object sender, system. eventargs E)
{
// Levin. jmail. Sendmail ("blue_lzg@163.com", "555555", "levin9@gmail.com", "title", "GGGG ");
Levin. util. jmail. Sendmail ("blue_lzg@163.com", "555555", "gtips@163.com", "title", "GGGG ");
}

Private void Button3_Click (object sender, System. EventArgs e)
{
Files. UpDown. UploadFile (FileUp, "test ");
// Levin. File. UpDown. SaveFile (FileUp, "text ");
}

Private void Button4_Click (object sender, System. EventArgs e)
{
Response. Write (Files. FileApi. Read (FileUp ));
}

Private void Button5_Click (object sender, System. EventArgs e)
{
Files. FileApi. write (FileUp, TextBox1.Text );
}

Private void Button6_Click (object sender, System. EventArgs e)
{
Files. FileApi. Append (FileUp, TextBox1.Text );
}

Private void Button7_Click (object sender, System. EventArgs e)
{
Response. Write (Files. FileApi. Reads (FileUp ));
}

Private void Button8_Click (object sender, System. EventArgs e)
{
Util. CNDate dt = new levin. util. CNDate (DateTime. Now );
Response. Write ("today is:" + dt. Date. ToString () + dt. GetConstellationName ());
Response. Write (dt. l_GetLunarHolDay ());
Console. WriteLine (dt. GetLunarHolDay ());
// Console. WriteLine ("" + dt. GetLeapMonth (UInt16.Parse (args [0]);
// Console. WriteLine ("days in December February" + dt. LunarMonthDays (UInt16.Parse (args [0]), UInt16.Parse (args [1]);
// Console. WriteLine ("days" + dt. LunarYearDays (UInt16.Parse (args [0]);
Response. Write ("" + dt. FormatLunarYear ());
Dt. Date = DateTime. Today. AddDays (1 );
Response. Write ("Tomorrow is:" + dt. Date. ToString () + dt. GetConstellationName ());
}

Private void Button9_Click (object sender, System. EventArgs e)
{
TextBox1.Text = Files. FileApi. Read (FileUp). ToString ();
}
}
}

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.