Source code of an instance of the ASPX File Upload restriction type

Source: Internet
Author: User

I want to share my ideas with you. I don't know if you have a better solution.

Source code:

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;

Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
// MyBlog: http://seay.sinaapp.com/
}
Protected void button#click (object sender, EventArgs e)
{
# Region file type determination
 
// Get the Upload File Name
String FileName = FileUpload1.FileName;
 
// Determine whether the file name contains wood.
If (! (FileName. Contains (".")))
{
Response. Write ("this file type cannot be uploaded! ");
Return;
}
 
// Obtain the subscript.
Int index = FileName. LastIndexOf ('.');

Char [] c = FileName. ToCharArray ();

String File_hz = "";
 
// Get the Suffix in a loop
For (int I = 0; I <FileName. Length-index; I ++)
{
File_hz + = c [index + I];
}

// File name that can be uploaded
String [] FileType = {". jpg", ". gif", ". bmp", ". jpeg "};

Bool bl = false;
 
// Cyclically traverse whether the uploaded file extension is in the allowed Extension
Foreach (string str in FileType)
{
If (str = File_hz)
{
Bl = true;
}
}

If (bl = false)
{
Response. Write ("this file type cannot be uploaded! ");
Return;
}

# Endregion

// Get the timestamp and name it in the file. This makes writing a bit complicated. I don't know if you have a good way to get the timestamp.
DateTime starttime = TimeZone. CurrentTimeZone. ToLocalTime (new System. DateTime (1970, 1, 1, 0, 0, 0, 0 ));
DateTime newtime = DateTime. Now;

Long utime = (long) Math. Round (newtime-starttime). TotalMilliseconds, MidpointRounding. AwayFromZero );

FileName = utime. ToString () + File_hz;

// Obtain the number of file bytes
String FileLenght = FileUpload1.PostedFile. ContentLength. ToString ();

String FilePath = Server. MapPath ("img/" + FileName );
 
// Upload
FileUpload1.SaveAs (FilePath );

Response. Write ("uploaded successfully <br/> file size:" + FileLenght + "<br/> path: img/" + 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.